我正在尝试按如下方式查询和排序文档:
但似乎因为$ geoNear默认限制为100个结果,所以我得到了意想不到的结果。
我看到$ geoNear按以下顺序工作:
有没有办法扭转秩序?
MongoDB v2.6.5
Java Driver v2.10.1
谢谢。
我的收藏中的示例文档:
{
"timestamp" : ISODate("2014-12-27T06:52:17.949Z"),
"text" : "hello",
"loc" : [
34.76701564815013,
32.05852053407342
]
}
我正在使用聚合,因为根据我的理解,它是按“时间戳”排序并获得距离的唯一方法。
BasicDBObject query = new BasicDBObject("timestamp", new BasicDBObject("$lt", SOMETIME));
// aggregate: geoNear
double[] currentLoc = new double[] {
Double.parseDouble(myLon),
Double.parseDouble(myLat)
};
DBObject geoNearFields = new BasicDBObject();
geoNearFields.put("near", currentLoc);
geoNearFields.put("distanceField", "dis");
geoNearFields.put("maxDistance", AROUNDME_RANGE_RADIUS_IN_RADIANS));
geoNearFields.put("query", query);
//geoNearFields.put("num", 5000); // FIXME: a temp solution I would really like to avoid
DBObject geoNear = new BasicDBObject("$geoNear", geoNearFields);
// aggregate: sort by timestamp
DBObject sortFields = new BasicDBObject("timestamp", -1);
DBObject sort = new BasicDBObject("$sort", sortFields);
// aggregate: limit
DBObject limit = new BasicDBObject("$limit", 20);
AggregationOutput output = col.aggregate(geoNear, sort, limit);
答案 0 :(得分:0)
您可以在管道顶部添加$match
阶段,以便在$geonear
阶段之前过滤文档。
BasicDBObject match = new BasicDBObject("timestamp",
new BasicDBObject("$lt", SOMETIME));
AggregationOutput output = col.aggregate(match,geoNear, sort, limit);
现在,下面的代码不是必需的,
geoNearFields.put("query", query);