【发布时间】:2014-07-19 18:22:03
【问题描述】:
我有一个包含from 和to 点位置的集合。现在我希望找到在给定源和目的地附近同时具有to 和from 位置的文档。
设置如下:
集合: db.t2.find():
{
"_id" : ObjectId("5..4"),
"uid" : "sdrr",
"valid_upto": 122334,
"loc" : {
"from" : {
"type" : "Point",
"coordinates" : [ 77.206672, 28.543347 ]
},
"to" : {
"type" : "Point",
"coordinates" : [ 77.1997687, 28.5567278 ]
}
}
}
索引:db.t2.getIndices():
{
"v" : 1,
"name" : "_id_",
"key" : {
"_id" : 1
},
"ns" : "mydb.t2"
},
{
"v" : 1,
"name" : "uid_1_loc.from_2dsphere_loc.to_2dsphere_valid_upto_1",
"key" : {
"uid" : 1,
"loc.from" : "2dsphere",
"loc.to" : "2dsphere",
"valid_upto" : 1
},
"ns" : "mydb.t2"
}
对to 或from 的单个查询在当前设置下效果很好。但是,当我在带有 $and 子句的单个查询中一起使用 to 和 from 时:
db.t2.find({
"$and" : [
{
"loc.from" : {
"$nearSphere" : [ 77.5454589,28.4621213 ],
"$maxDistance" : 0.18
}
},
{
"loc.to" : {
"$nearSphere" : [ 77.206672, 28.543347 ],
"$maxDistance" : 0.18
}
}
]
})
它会抛出以下错误:
错误:{
"$err" : "找不到任何特殊索引: 2d (needs index), 2dsphere (needs index), for: { $and: [ { loc.from: { $nearSphere: [ 77.5454589, 28.4621213 ], $maxDistance : 0.18 } }, { loc.to: { $nearSphere: [ 77.206672, 28.543347 ], $maxDistance: 0.18 } } ] }",
“代码”:13038
}
我想数据已经从 getIndices() 中明显地被索引了,但它仍然无法找到索引!那么问题出在哪里,我该如何解决它以使$and-ed 操作生效?
【问题讨论】:
标签: mongodb indexing geospatial