python - Mongoengine filter query on list embedded field based on last index -
i'm using mongoengine django.
i have embedded field in model. list field of embedded documents.
import mongoengine class orderstatuslog(mongoengine.embeddeddocument): status_code = mongoengine.stringfield() class order(mongoengine.dynamicdocument): incr_id = mongoengine.sequencefield() status = mongoengine.listfield(mongoengine.embeddeddocumentfield(orderstatuslog)) now want filter result on order collection based on last value in status field.
e.g. order.objects.filter(status__last__status_code="scode")
i guess there no such thing __last. tried approach mentioned in docs http://docs.mongoengine.org/guide/querying.html#querying-lists didn't work.
i can solve looping on documents in collection thats not efficient, how can write query efficiently.
i'm not sure mongoengine can (yet). afaik, you'd need use aggregation pipeline.
in mongo shell, using '$slice' , $arrayelemat operators:
db.order.aggregate([{ $project: {last_status: { $arrayelemat: [{ $slice: [ "$status", -1 ] }, 0 ]} }}, {$match: {'last_status.status_code':"scode"}} ]) and in python:
pipeline = [ {'$project': {'last_status': { '$arrayelemat': [{ '$slice': [ "$status", -1 ] }, 0 ]} }}, {'$match': {'last_status.status_code':'scode'}} ] agg_cursor = order.objects.aggregate(*pipeline) result = [ order.objects.get(id=order['_id']) order in agg_cursor ] the trick here objects.aggregate provides pymongo cursor, not mongoengine cursor, if need mongoengine objects, can proceed in 2 steps: first filter using aggregation framework ids of matched items, them through mongoengine query.
this do. tests, had proven more efficient fetching , filtering in python code.
if there simpler way, i'm interested ear it. otherwise, feature request mongoengine. may want open issue there.
Comments
Post a Comment