Wednesday, 15 January 2014

python - How to sort by other field in $geonear aggregation pipeline in mongodb -



python - How to sort by other field in $geonear aggregation pipeline in mongodb -

my collection has format.

{ "_id" : objectid("52e5f94d83b45407f959e7ff"), "latlng" : { "coordinates" : [ 85.29035240000007, 27.6663671 ], "type" : "point" }, "name" : "sujit maharjan", "updates" : [ { "status" : "i want #buy 5 kg of tomatoes.", "picture" : [ ], "parent_tweet_id" : "0", "deleted" : 1, "tweet_id" : "428578269169205248", "time_stamp" : 1391015996 } { "status" : "i want #start #milk business can help me ?", "picture" : [ ], "parent_tweet_id" : "0", "deleted" : 0, "tweet_id" : "108fd43a-7efa-404d-800d-0c30a5da06e5", "time_stamp" : 1391955084 }, { "status" : "@santoshghimire @bhanduroshan connect #dairy business", "picture" : [ ], "parent_tweet_id" : "432503201968168960", "deleted" : 1, "tweet_id" : "432517594026082304", "time_stamp" : 1391955208 }, { "status" : "@bhanduroshan did message ?", "picture" : [ ], "parent_tweet_id" : "432502654154334208", "deleted" : 0, "tweet_id" : "432788670463377408", "time_stamp" : 1392019838 }, { "status" : "this tweet images @foodtradehq http://t.co/3el1351hwf", "picture" : [ "http://pbs.twimg.com/media/bglz4yacuaasftj.jpg" ], "parent_tweet_id" : "0", "deleted" : 1, "tweet_id" : "433148076820156417", "time_stamp" : 1392105574 } ] }

now need query updates in users within radius sorted updates.time_stamp.

for used aggregation pipeline $geonear query sort distance , limit result.

this pipeline in python

geo_search = {"near": [float(self.lng), float(self.lat)], "distancefield": "distance", "includelocs": "latlng", "uniquedocs": true, "spherical":true, "limit":100, # cutting off possible results, , complexity increasing in increasing number } pipeline = [] final_query = {"$and":query_string} if len(query_string)>0: geo_search['query'] = final_query geo_search['maxdistance'] = 0.01261617096 geo_near = { "$geonear": geo_search } pipeline.append(geo_near)

with $geonear aggregation pipeline stage, standard type of "nearsphere" or "near" query places result documents in pipeline additional field required distancefield.

this must first pipeline stage, can utilize index:

collection.aggregate([ { "$geonear": { "near": [ float(self.lng), float(self.lat) ], "maxdistance": 0.01261617096, "distancefield": "distance", "includelocs": "latlng", "uniquedocs": true, "spherical":true, "query": { "updates.time_stamp": { "$gte": timestamp_cutoff } }, "limit":100 }}, { "$sort": { "other": 1, "distance": 1 } } ])

through rest of pipeline there additional field defined in "distancefield", in order nearest results pass $sort. , can pass whatever want sort pipeline stage does.

you can deed on results including additional stages $match etc. of course of study if other info relevant initial result utilize "query" alternative $geonear.

really want need "limit" possible documents matched using in "query" much shown. "nearest" documents returned match additional criteria.

python mongodb pymongo aggregation-framework

No comments:

Post a Comment