首页 > 解决方案 > 具有 nearSphere 条件的 MongoDB 查找表达式

问题描述

我有一个场景,我必须根据他们的位置加入我的餐厅和拉链系列。

餐厅

[{
    "_id" : ObjectId("55cba2486c522cafdb059deb"),
    "location" : {
        "coordinates" : [
            -74.138492,
            40.631136
        ],
        "type" : "Point"
    },
    "name" : "Indian Oven"
}]

拉链

[{
    "_id" : "99950",
    "city" : "KETCHIKAN",
    "loc" : [
        -133.18479,
        55.942471
    ],
    "pop" : 422,
    "state" : "AK"
}]

现在我必须加入这两个基于 nearSphere 地理空间查询的集合。我应该在 $lookup 管道中使用什么标准。

我尝试的是

db.restaurants.aggregate([
    {
        $lookup:
            {
                from: "zips",
                let: { location: "$location" },
                pipeline: [{
                    $match: {
                        'loc': { $nearSphere: '$$location' }
                    }
                }],
                as: "t"
            }
    }
]);

但这不起作用。

需要帮忙

标签: mongodbmongodb-queryaggregation-frameworkgeospatialmongodb-geospatial

解决方案


推荐阅读