通过geoNear获取子文档-MongoDB [英] Gets sub documents by geoNear - MongoDB

查看:119
本文介绍了通过geoNear获取子文档-MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况。

在我们的数据库中,商店文档和分支机构作为子文档(一对多)。

In our database have "stores" documents, and "branch offices" as subdocuments ( One to Many ).

每个分支机构都有一个location属性,该属性被索引以进行地理位置搜索。

Each branch office have a location attribute, which is indexed for geolocation searching.

因此,问题是:

{store:{"name":"store1", branchoffices:[{"name":"bo1","location":[ -70.64341379999999, -33.4268697 ]}, {"name":"bo2","location":[80.4,43.3]}]}}

如果我执行此聚合:

Store.collection.aggregate(
        [{
            "$geoNear"=>{"near"=>[ -70.64341379999999, -33.4268697 ], 
            "distanceField"=>"distance", 
            "maxDistance"=>0.0900899926955034}
        },
        { "$unwind" => "$branchoffices"}
    ]

结果是每个分支机构都在两行或返回的记录中重复了距离字段。 d在geoNear中有一个分支机构。

The result it's each branch office with the distance field repeated in both rows or records returned. When just found one branchoffice in geoNear.

是否存在某种方式,只返回地理搜索的子文档?

Exists some way for just return the subdocument or subdocuments that result for the geolocation searchs ?

谢谢。

推荐答案

$ geoNear includeLocs 如下:

Store.aggregate([
    { "$geoNear": {
        "near": [ -70.64341379999999, -33.4268697 ],
        "distanceField": "distance", 
        "maxDistance": 0.0900899926955034,
        "includeLocs": "location"
    }}
])

输出在输出字段中具有与距离匹配的位置:

The output has the matched "location" to the "distance" in the output field:

{
    "_id" : ObjectId("5507b18d1c3bdce0535aecd0"),
    "name" : "store1",
    "branchoffices" : [
            {
                    "name" : "bo1",
                    "location" : [
                            -70.64341379999999,
                            -33.4268697
                    ]
            },
            {
                    "name" : "bo2",
                    "location" : [
                            80.4,
                            43.3
                    ]
            }
    ],
    "distance" : 0,
    "location" : [
            -70.64341379999999,
            -33.4268697
    ]
}

如果您想要使用的数组中的特定子文档在详细比赛中,您可以使用 $ redact

If you wanted the specific sub-document in the array that was used in the match in full detail then you could continue with a filter using $redact:

Store.aggregate([
    { "$geoNear": {
        "near": [ -70.64341379999999, -33.4268697 ],
        "distanceField": "distance", 
        "maxDistance": 0.0900899926955034,
        "includeLocs": "location"
    }},
    { "$redact": {
        "$cond": [
            { "$eq": [ "$location", "$$ROOT.location" ] },
            "$$DESCEND",
            "$$PRUNE"
        ]
    }}
])

这样的MongoDB 2.6:

Or in versions prior to MongoDB 2.6 like this:

Store.aggregate([
    { "$geoNear": {
        "near": [ -70.64341379999999, -33.4268697 ],
        "distanceField": "distance", 
        "maxDistance": 0.0900899926955034,
        "includeLocs": "location"
    }},
    { "$unwind": "$branchoffices" },
    { "$project": {
        "name": 1,
        "branchoffices": 1,
        "matched": {
            "$eq": [ "$location", "$branchoffices.location" ]
        }
    }},
    { "$match": { "matched": 1 } },
    { "$group": {
        "_id": "$_id",
        "name": { "$first": "$name" },
        "branchoffices": { "$push": "$branchoffices" },
        "distance": { "$first" "$distance" }
    }}
])

您可能应该注意,在子文档中使用对象并不总是一种最佳解决方案,而是通常不适合各种任务。例如,如果数组中的数据可能包含要在查询点附近的多个位置,则只有奇异的最近点才能像这样匹配。

You should probably note that using objects within a sub-document is not always an optimal solution and is generally not suited to a variety of tasks. For example, if your data in the array possibly contained "multiple" locations that would be "near" the queried point, then only the singular "nearest" point would be able to be matched like this.

因此,尽管您可以执行此操作,但最好考虑一下它的使用方式以及预期的结果。在大多数情况下,位置数据应在自己的文档中列出,而不是像此处那样在子文档数组下列出。

So whilst you can do this, it is best to consider how you are using it and the results you expect. In most cases, location data should be listed in it's own document rather than under a sub-document array as is done here.

这篇关于通过geoNear获取子文档-MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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