$ geoNear匹配最近的数组 [英] $geoNear matching nearest array
问题描述
我在Mongodb(3.4)的记录集合中有文档,如下所示:-
I have document in record collection Mongodb (3.4) like below :-
{
"_id" : ObjectId("592d0c78555a7436b0883960"),
"userid" : 7,
"addresses" : [
{
"apporx" : 50.0,
"loc" : [
-73.98137109999999,
40.7476039
]
},
{
"apporx" : 15.0,
"loc" : [
-73.982002,
40.74767
]
},
{
"apporx" :10.0,
"loc" : [
-73.9819567,
40.7471609
]
}
]
}
`
I created index on this collection using below query :-
`db.records.createIndex({'addresses.loc':1})`
when i execute my below query :-
`db.records.aggregate(
{$geoNear : {
near : [ -73.9815103, 40.7475731 ],
distanceField: "distance"
}});
这个结果给了我距离字段.现在您可以在我的文档中解释一下此多个元素中的哪个地址数组.我如何确定该结果对哪个元素为真?
this result gives me distance field.now can you explain me in my document which address array in this multiple elements exists. how can i determine for which element this result true?
另一个问题:-如果我在"addresses.apporx"上设置条件,例如大于或等于,那么有什么方法可以找到此条件的位置吗?
Another question :- if i make condition on "addresses.apporx" like greater then or equal then is there any way to find location of this condition?
推荐答案
首先,我强烈建议您如果要对真实世界坐标进行地理空间查询,请为您的集合创建一个"2dsphere"索引.
Firstly I strongly advise that you create a "2dsphere" index for your collection if you intend to to geospatial queries on real world coordinates.
请确保删除您可能一直在玩的其他索引:
Make sure to drop other indexes you may have been playing with:
db.records.dropIndexes();
db.records.createIndex({ "addresses.loc": "2dsphere" })
In order to do what you want, first take a look at the slight modification that also includes the includeLocs option to $geoNear
db.records.aggregate([
{ "$geoNear": {
"near": [ -73.9815103, 40.7475731 ],
"spherical": true,
"distanceField": "distance",
"includeLocs": "locs"
}}
])
现在,您将看到如下所示的输出:
Now you will see output that looks like this:
{
"_id" : ObjectId("592d0c78555a7436b0883960"),
"userid" : 7,
"addresses" : [
{
"apporx" : 50,
"loc" : [
-73.98137109999999,
40.7476039
]
},
{
"apporx" : 15,
"loc" : [
-73.982002,
40.74767
]
},
{
"apporx" : 10,
"loc" : [
-73.9819567,
40.7471609
]
}
],
"distance" : 0.0000019174641401278624,
"locs" : [
-73.98137109999999,
40.7476039
]
}
所以返回的不仅是到最近点的距离,还包括所使用的匹配是哪个"位置.
So what that returned was not only the distance to the point that was nearest but "which" location was the match used.
因此,如果您想 $filter
数组以返回最接近的值,那么您可以:
So if you wanted to $filter
the original array to return the nearest, then you can:
db.records.aggregate([
{ "$geoNear": {
"near": [ -73.9815103, 40.7475731 ],
"spherical": true,
"distanceField": "distance",
"includeLocs": "locs"
}},
{ "$addFields": {
"addresses": {
"$filter": {
"input": "$addresses",
"as": "address",
"cond": { "$eq": [ "$$address.loc", "$locs" ] }
}
}
}}
])
然后返回仅具有该匹配项的数组:
And that returns the array with only that match:
{
"_id" : ObjectId("592d0c78555a7436b0883960"),
"userid" : 7,
"addresses" : [
{
"apporx" : 50,
"loc" : [
-73.98137109999999,
40.7476039
]
}
],
"distance" : 0.0000019174641401278624,
"locs" : [
-73.98137109999999,
40.7476039
]
}
这篇关于$ geoNear匹配最近的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!