$geoNear 匹配最近的数组 [英] $geoNear matching nearest array

查看:11
本文介绍了$geoNear 匹配最近的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在记录集合 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" })

为了做你想做的事,首先看看还包括includeLocs选项的轻微修改$geoNear

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆