【问题标题】:Mongodb Stitch unsupport $geoNear in collection.aggregate(). Alternative $geoWithin does not have includeLocsmongodb Stitch 在 collection.aggregate() 中不支持 $geoNear。替代 $geoWithin 没有 includeLocs
【发布时间】:2019-04-11 15:31:24
【问题描述】:

$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 文档中返回特定字段?

谢谢。

【问题讨论】:

    标签: mongodb mongodb-stitch


    【解决方案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();
    

    【讨论】:

    • geoNear 是 depr,但 $geoNear 不是,并且目前没有其他方法可以在聚合管道中进行位置查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2015-11-18
    • 2020-08-29
    • 2013-06-29
    相关资源
    最近更新 更多