$geoNear matching nearest array建议使用$ geoNear及其选项“ includeLocs”。但是,MongoDb Stitch函数不支持此聚合。您可以阅读文档https://docs.mongodb.com/stitch/mongodb/actions/collection.aggregate/#unsupported-aggregation-stages
在MongoDb Stitch中,我可以使用$ geoWithin轻松查询地理空间。但是我不能分开我的回报模型。是否有其他替代方法“ includeLocs”,或者如何过滤$ geoWithin结果?
我的模特是:
[
{
"locations": [
{
"address": "bla bla bla",
"city": "Avila Beach",
"coordinates": [
-120.73605,
35.17998
],
"country": "Usa",
"prods": [
{
"brand": "kumcho",
"cagtegory": "tire",
"id": "1_kumcho"
},
{
"brand": "micheline",
"cagtegory": "tire",
"id": "1_micheline"
},
{
"brand": "setrr",
"cagtegory": "rim",
"id": "1_setrr"
}
],
"state": "5 Cities"
},
{
"address": "data data data",
"city": "Arvin",
"coordinates": [
-118.84151,
35.21617
],
"country": "Usa",
"prods": [
],
"state": "Bakersfield",
}
],
"name": "My Car",
"admin": "John"
}
]
根据此模型,当我如下查询时。
.find({"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}},{"locations.coordinates":1})
.toArray()
其他替代方案。
const pipeline = [
{$match: {"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}}}
];
const cursor = coll.aggregate(pipeline);
结果是:
[
{
"locations": [
{
"coordinates": [
{
"$numberDouble": "-120.73605"
},
{
"$numberDouble": "35.17998"
}
]
},
{
"coordinates": [
{
"$numberDouble": "-118.84151"
},
{
"$numberDouble": "35.21617"
}
]
}
]
}
]
但是我想要带有根的文档的特定数组对象,该对象与搜索到的坐标匹配。
例如,我需要类似的东西。
搜索值:
lon = -120.55361687164304
lat = 35.22037830812648
kmml = 3963.2
(这是英里)
distance = 15
根据上面的搜索值,匹配的模型坐标为:
"coordinates": [-120.73605, 35.17998]
所以我想要这样的结果:
[
{
"locations": [
{
"address": "bla bla bla",
"city": "Avila Beach",
"coordinates": [
-120.73605,
35.17998
],
"country": "Usa",
"prods": [
{
"brand": "kumcho",
"cagtegory": "tire",
"id": "1_kumcho"
},
{
"brand": "micheline",
"cagtegory": "tire",
"id": "1_micheline"
},
{
"brand": "setrr",
"cagtegory": "rim",
"id": "1_setrr"
}
],
"state": "5 Cities"
}
],
"name": "My Car",
"admin": "John"
}
]
是否可以在mongodb文档中返回特定字段?
谢谢。
答案 0 :(得分:-1)
geoNear从版本4.0开始不推荐使用。您可以尝试使用$ near $ geometry; https://docs.mongodb.com/manual/reference/operator/query/near/
//Mile-Km to meters
if (kmml == 3963.2){
distance = distance * 1609.344;
} else{
distance = distance * 1000;
}
return coll
.find({"location":{
$near: {
$geometry: {
type : "Point",
coordinates : [ lon, lat ]
},
$maxDistance: distance
}
}
}).toArray();