首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$lookup之后的Mongodb $near

$lookup之后的Mongodb $near
EN

Stack Overflow用户
提问于 2016-10-24 05:59:58
回答 1查看 624关注 0票数 2

如果位置坐标在另一个集合中,我如何使用$near查询服务?

$lookup,$near

我有2个收藏,一个是给用户的,另一个是服务的。

用户集合为:

代码语言:javascript
复制
{ "_id" : ObjectId("570557d4094a4514fc1291d6"), "fullname" : "John Doe",
"position" : {
    "street" : "Piazza Santa Maria",
    "city" : "Busto",
    "country" : "Italy",
    "zip_code" : "21052",
    "address" : "Piazza Santa Maria, 21052 Busto, Province of Varese, Italy",
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            8.8502715, 
            45.6114064
        ]
    },
    "country_code" : "IT",
    "state" : "Lombardy",
    "province" : "Province of Varese"
 }
}

}

服务集合为:

代码语言:javascript
复制
{ "_id" : ObjectId("960557d4094a4514fc1291a7"), 
author_id : ObjectId("570557d4094a4514fc1291d6"), title:"Software developer"}
{ "_id" : ObjectId("960557d4094a4514fc1291a7"), 
author_id : ObjectId("570557d4094a4514fc1291d6"), title:"App developer"}

我希望根据作者位置查找服务集合中的所有文档。

EN

回答 1

Stack Overflow用户

发布于 2016-10-24 08:44:44

首先,您需要满足基于位置的MongoDB查询的两个需求。

在 collection

  1. $geoNear阶段上创建users
  2. 索引应该是聚合管道中的第一个阶段。

示例:

代码语言:javascript
复制
db.users.aggregate([
  {
    $geoNear: {
      near: {type: "Point", coordinates: [ 8.840 , 45.6114060 ]},
      distanceField: "dist.calculated",
      maxDistance:1000,
      spherical: true
  }},
  {
    $lookup: {
      from:"services", localField:"_id", foreignField:"author_id", as: "services"
  }}
])

在这里,我们试图找到落在1000米范围内的文档。在本例中,文档的位置为[8.8502715, 45.6114064]

输出

代码语言:javascript
复制
{ 
    "_id" : ObjectId("570557d4094a4514fc1291d6"), 
    "fullname" : "John Doe", 
    "position" : {
        "street" : "Piazza Santa Maria", 
        "city" : "Busto", 
        "country" : "Italy", 
        "zip_code" : "21052", 
        "address" : "Piazza Santa Maria, 21052 Busto, Province of Varese, Italy", 
        "location" : {
            "type" : "Point", 
            "coordinates" : [
                8.8502715, 
                45.6114064
            ]
        }, 
        "country_code" : "IT", 
        "state" : "Lombardy", 
        "province" : "Province of Varese"
    }, 
    "dist" : {
        "calculated" : 799.8404739526889
    }, 
    "services" : [
        {
            "_id" : ObjectId("960557d4094a4514fc1291a7"), 
            "author_id" : ObjectId("570557d4094a4514fc1291d6"), 
            "title" : "Software developer"
        }, 
        {
            "_id" : ObjectId("580d499eb22270301913d677"), 
            "author_id" : ObjectId("570557d4094a4514fc1291d6"), 
            "title" : "Software developer"
        }, 
        {
            "_id" : ObjectId("580d499eb22270301913d678"), 
            "author_id" : ObjectId("570557d4094a4514fc1291d6"), 
            "title" : "App developer"
        }
    ]
}

如果我们在google地图上绘制这两个文档以进行验证,我们将得到

正如您在图像上看到的,我们的文档位置大约相隔800米,因此在query中指定的1000米范围内。请参阅Goole map

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40208218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档