示例 MongoDB 文档
{
"_id":"5adedaad0bd4134kb0",
"url":"https://iscon.cdnalign.com/t51.288515/s640x640e35/10249200241120_2108192950_n.jpg?ig_cache_key=MTEYUTB1NDgxN",
"description":"#funinthesun",
"locationName":"Calamari",
"statusValue":1,
"reason":"Related to travel",
"category":["park"],
"geo": {
"index":"Point",
"coord":[29.123024,77.1999]
}
}
我想根据与特定点的距离来获取文档。
这是我正在使用的查询,以便文档根据距离出现。
询问
var collection = db.collection('myData');
collection.ensureIndex({"geo.index":"Point"});
collection.find({
geo :{
$near : {
$geometry : {
index : "Point" ,
coord : [30.564058, 76.44762696]
},
$maxDistance : 100
}
}
}
这向我展示了这个错误:-
{"name":"MongoError","message":"geo near query $geometry 参数中的无效点:{ index:\"Point\", coord: [ 4.27326978424058, 30.4439024447627 ] } 点必须是数组或对象", "waitedMS":0,"ok":0,"errmsg":"geo near query $geometry 参数中的无效点:{ index:\"Point\", coord: [ 4.27326978424058, 30.4439024447627 ] } 点必须是一个数组或对象","代码":2}
最佳答案
如果您查看 $near 文档 https://docs.mongodb.com/manual/reference/operator/query/near/ 您会发现这里有两个问题:
这是您的代码的正确版本,应该可以正常工作
var collection = db.collection('myData');
collection.ensureIndex({"geo":"2dsphere"});
collection.find({
geo :{
$near : {
$geometry : {
index : "Point" ,
coordinates : [30.564058, 76.44762696]
},
$maxDistance : 100
}
}
})
关于实现 $near 查询时 MongoDB 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39147277/