MongoDB在两个基于地理点的集合上汇总

发布于 2025-02-11 12:21:32 字数 2046 浏览 0 评论 0原文

我有两个收藏品

Person

{
    "name": "Mike",
    "age": 42,
    "address": {
         "location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
    }
}

餐厅

{
    "name": "Bistro",
    "type": ["Maxican", "Italian"],
    "location": {"type": "Point", "coordinates": [12.3555, 78.9333]} 
}

我在这两个收藏中都创建了球形索引,

db.person.createIndex({"address.location": "2dsphere"})
db.restaurant.createIndex({"location": "2dsphere"})

现在我想在两个收藏中使用Lat/Long的人附近找到所有餐馆。 因为例如,在一个人的10公里以内找到所有餐厅。结果将看起来像下面的情况

{
    "name": "Mike",
    "age": 42,
    "address": {
         "location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
    },
    "nearby_restaurants": [{"name": "Bistro", "distance": 5.3}]  // this means bistro is 5.3 KM away from this person
}

,请注意,我仅使用name从餐厅集合中使用的字段和其他字段decort将在查询后计算。

我想为所有人做这个。因此,基本上,对于每个人,我每次都必须扫描整个餐厅系列。 为了实现这一目标,我想到将查找与汇总一起使用,在下面类似的情况下

db.person.aggregate([
    {
        $lookup: {
            from: "restaurant",
            let: {"personPoint": "$address.location"},
            as: "nearby_restaurants",
            pipeline: [
                {
                    $geoNear: {
                        near: "$$personPoint",
                        spherical: true,
                        distanceField: "distance",
                        maxDistance: 10 * 1000,  // within 10 KM
                        distanceMultiplier: 0.001  // get the result in KM
                    }
                },
                {
                    $unwind: "$location"
                },
            ]
        }
    }, 
    {
        $unwind: {
            path: "$nearby_restaurants",
            preserveNullAndEmptyArrays: true
        }
    }
])

根本不起作用。我得到了这个错误: $ geonear需要一个“接近”选项作为一个数组”

我试图查看不同的来源,但无法理解和解决错误。PS

:如果目前,通过查找/汇总,我们如何通过循环浏览每个人的文档来实现同样的事情?

I have two collections

person

{
    "name": "Mike",
    "age": 42,
    "address": {
         "location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
    }
}

restaurant

{
    "name": "Bistro",
    "type": ["Maxican", "Italian"],
    "location": {"type": "Point", "coordinates": [12.3555, 78.9333]} 
}

I've created spherical indexes on both collections

db.person.createIndex({"address.location": "2dsphere"})
db.restaurant.createIndex({"location": "2dsphere"})

Now I want to find all the restaurants near to a person using lat/long in both collections.
for e.g find all the restaurants within 10KM of a person. result will look like below

{
    "name": "Mike",
    "age": 42,
    "address": {
         "location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
    },
    "nearby_restaurants": [{"name": "Bistro", "distance": 5.3}]  // this means bistro is 5.3 KM away from this person
}

Notice that, I'm only using name field from restaurant collection and an additional field distance which will be calculated after the query.

I want to do this for all person. so basically for each person, i have to scan the whole restaurant collection every time.
To achieve this I thought of using lookup with aggregate, something like below

db.person.aggregate([
    {
        $lookup: {
            from: "restaurant",
            let: {"personPoint": "$address.location"},
            as: "nearby_restaurants",
            pipeline: [
                {
                    $geoNear: {
                        near: "$personPoint",
                        spherical: true,
                        distanceField: "distance",
                        maxDistance: 10 * 1000,  // within 10 KM
                        distanceMultiplier: 0.001  // get the result in KM
                    }
                },
                {
                    $unwind: "$location"
                },
            ]
        }
    }, 
    {
        $unwind: {
            path: "$nearby_restaurants",
            preserveNullAndEmptyArrays: true
        }
    }
])

This doesnt work at all. I get this error: $geoNear requires a 'near' option as an Array"

I tried to look at different sources but couldnt understand and fix the error.

PS: If currently, it's not possible through lookups/aggregate, how can we achieve the same through looping over each person's document?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文