【发布时间】:2014-08-14 12:39:53
【问题描述】:
我的收藏有这种格式。
{
"_id" : ObjectId("52e5f94d83b45407f959e7ff"),
"latlng" : {
"coordinates" : [
85.29035240000007,
27.6663671
],
"type" : "Point"
},
"name" : "Sujit Maharjan",
"updates" : [
{
"status" : "I want to #buy 5 kg of tomatoes.",
"picture" : [ ],
"parent_tweet_id" : "0",
"deleted" : 1,
"tweet_id" : "428578269169205248",
"time_stamp" : 1391015996
}
{
"status" : "I want to #start #milk business who can help me ?",
"picture" : [ ],
"parent_tweet_id" : "0",
"deleted" : 0,
"tweet_id" : "108fd43a-7efa-404d-800d-0c30a5da06e5",
"time_stamp" : 1391955084
},
{
"status" : "@SantoshGhimire @bhanduroshan Connect to us for #Dairy business",
"picture" : [ ],
"parent_tweet_id" : "432503201968168960",
"deleted" : 1,
"tweet_id" : "432517594026082304",
"time_stamp" : 1391955208
},
{
"status" : "@bhanduroshan Did you get my message ?",
"picture" : [ ],
"parent_tweet_id" : "432502654154334208",
"deleted" : 0,
"tweet_id" : "432788670463377408",
"time_stamp" : 1392019838
},
{
"status" : "this is tweet with 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
}
]
}
现在我需要查询用户在某个半径内的更新,按updates.time_stamp排序。
为此,我使用了聚合管道,但 $geonear 查询会从距离排序并限制结果。
这是我在 python 中的管道
geo_search = {"near": [float(self.lng), float(self.lat)],
"distanceField": "distance",
"includeLocs": "latlng",
"uniqueDocs": True,
"spherical":True,
"limit":100, # this will cut off the possible results, and complexity increasing in increasing this 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)
【问题讨论】:
标签: python mongodb mongodb-query aggregation-framework pymongo