我一直在使用MongoDB2.4.3作为一个社交网络应用程序,它根据呼叫者的距离返回配置文件。我使用了$nearSphere操作符如下:
db.profiles.find({loc: {$nearSphere: <lon, lat>}} ...).skip(0).limit(100);我知道$nearSphere操作符只返回100个结果,但是它确实按照我需要的距离对结果集进行排序,而且由于我的应用程序页面的倍数为100,所以我能够按距离迭代概要文件列表,只需按以下方式递增skip值:
db.profiles.find({loc: {$nearSphere: <lon, lat>}} ...).skip(100).limit(100);
db.profiles.find({loc: {$nearSphere: <lon, lat>}} ...).skip(200).limit(100);
db.profiles.find({loc: {$nearSphere: <lon, lat>}} ...).skip(300).limit(100);这已经有一年的时间了。最近,我将其升级到2.6.1,并且注意到,现在,无论我传递什么跳过值,都只返回前100个结果。此后,我不得不将其降级到v2.4.3,以便恢复此功能。
我的问题是,这种行为是2.4.3中的bug还是2.6.1中的bug?据我所知,医生们没有提到这个变化。如果我碰巧利用了2.4.3中的一个bug,那么在保持相同功能的同时,最佳的适应方式是什么呢?谢谢!
发布于 2014-06-20 18:02:57
您状态的100限制仅用于“2d”索引,因此我猜您使用的是“2d”索引。从我在版本2.6.3 (和2.6.0,在更新到最新版本之前)中测试的内容来看,似乎存在一个错误,这个特定查询的限制没有考虑到跳过,所以如果您跳过100,那么应该使用100 + limit限制。
复制(在mongo客户端):
db.test.drop();
db.test.ensureIndex({point: "2d"});
for (var i = 0; i < 200; ++i) {
db.test.insert({point:[1,2]});
}
db.test.insert({point:[1,2.01]});
db.test.find({point: {$nearSphere: [1,2]}}).skip(200).limit(201);返回:
{ "_id“:ObjectId("53a476c6c83c83ebdd121038"),”点“:1,2.01 }
当你
db.test.find({point: {$nearSphere: [1,2]}}).skip(200).limit(1);什么都没有。
为了比较,当你
db.test.find({}).skip(200).limit(1)它回来了
{ "_id“:ObjectId("53a476c6c83c83ebdd121038"),”点“:1,2.01 }
对于“2 2dsphere”,它可以像您预期的那样工作。
https://stackoverflow.com/questions/24332586
复制相似问题