1

$geoNear在此问题中匹配最近的数组建议使用 $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 文档中返回特定字段?

谢谢你。

4

1 回答 1

0

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();
于 2018-11-26T13:52:11.697 回答