mongoose MongoDB $geoNear -为什么我只得到一个结果,而在指定的半径内应该有更多的结果?

ybzsozfc  于 2023-10-19  发布在  Go
关注(0)|答案(1)|浏览(112)

我在一个应用程序中使用了基于位置的搜索功能,我设置了5英里的半径来查找附近的地方。然而,我感到困惑,因为我只得到一个结果,当我知道应该有更多的企业或地点在该半径。
我已经仔细检查了查询中的坐标和半径参数,它们似乎是正确的。是否还有其他我应该考虑或检查的事情,以确保在指定的半径内收到所有预期的结果?任何帮助或建议将不胜感激!
谢谢你,谢谢!

这里是我的代码:

db.getCollection("companies").aggregate([
    {
        $geoNear: {
            near: {
                type: "Point",
                coordinates: [11.09576, 47.49209]
            },
            distanceField: "erfuellungsorte.distance",
            maxDistance: 100000,
            spherical: true,
            distanceMultiplier: 0.001
        }
    },
    {
        $unwind: "$erfuellungsorte"
    },
    {
        $addFields: {
            "erfuellungsorte._id": { $first: "$erfuellungsOrte._id" },
            "erfuellungsorte.erfuellungsOrt": { $first: "$erfuellungsOrte.erfuellungsOrt" },
            "erfuellungsorte.facebook": { $first: "$erfuellungsOrte.facebook" },
            "erfuellungsorte.instagram": { $first: "$erfuellungsOrte.instagram" },
            "erfuellungsorte.tiktok": { $first: "$erfuellungsOrte.tiktok" },
            "erfuellungsorte.location": { $first: "$erfuellungsOrte.location" },
            "erfuellungsorte.logoBilderDetails": { $first: "$erfuellungsOrte.logoBilderDetails" },
            "erfuellungsorte.email": { $first: "$erfuellungsOrte.email" },
            "erfuellungsorte.entwertungs_email": { $first: "$erfuellungsOrte.entwertungs_email" },
            "erfuellungsorte.erfuellungsOrt": { $first: "$erfuellungsOrte.erfuellungsOrt" },
            "erfuellungsorte.speisekarte": { $first: "$erfuellungsOrte.speisekarte" },
            "erfuellungsorte.oeffnungszeiten": { $first: "$erfuellungsOrte.oeffnungszeiten" },
            "erfuellungsorte.website": { $first: "$erfuellungsOrte.website" },
            "erfuellungsorte.whatsapp": { $first: "$erfuellungsOrte.whatsapp" },
            "erfuellungsorte.strasse": { $first: "$erfuellungsOrte.strasse" },
            "erfuellungsorte.ort": { $first: "$erfuellungsOrte.ort" },
            "erfuellungsorte.land": { $first: "$erfuellungsOrte.land" },
            "erfuellungsorte.ausstattungsmerkmaleDetails": { $first: "$erfuellungsOrte.ausstattungsmerkmaleDetails" }
        }
    },
    {
        $group: {
            _id: "$_id",
            kundennummer: { $first: "$kundennummer" },
            logoBilderDetails: { $first: "$logoBilderDetails" },
            angebotseinstellungenDetails: { $first: "$angebotseinstellungenDetails" },
            state: { $first: "$state" },
            erfuellungsorte: {
                $push: "$erfuellungsorte"
            }
        }
    },
    {
        $project: {
            _id: 1,
            kundennummer: 1,
            state: 1,
            erfuellungsorte: 1
        }
    }
]);

这是我在数据库中的Mongodb文档:

{
    "_id" : ObjectId("651c93cb7d6aabe8e5abffcf"),
    "kundennummer" : "5663732",
    "state" : "DRAFT",
    "kundentyp" : "NEUKUNDE",
    "reviews" : [

    ],
    "documentLinks" : {
        "vertragLink" : "Beispiel-Vertragslink",
        "beratungsprotokollLink" : "Beispiel-Beratungsprotokoll-Link",
        "vollmachtLink" : "Beispiel-Vollmacht-Link",
        "_id" : ObjectId("651cad07c493da4613ff123b"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "betreiberDetails" : {
        "rechtsform" : "Beispiel-Rechtsform",
        "firma" : "Beispiel-Firma",
        "strasse" : "Beispiel-Straße",
        "ort" : "Beispiel-Ort",
        "postleitzahl" : "12345",
        "land" : "Beispiel-Land",
        "telefon" : "1234567890",
        "mobil" : "9876543210",
        "whatsapp" : "9876543210",
        "email" : "[email protected]",
        "vertretung_anrede" : "Herr",
        "vertretung_vorname" : "Max",
        "vertretung_nachname" : "Mustermann",
        "vertretung_position" : "Beispiel-Position",
        "_id" : ObjectId("651cad08c493da4613ff1249"),
        "createdAt" : ISODate("2023-10-04T00:08:40.012+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:40.012+0000")
    },
    "kommunikationsanschriftDetails" : {
        "firma" : "Beispiel-Firma",
        "strasse" : "Beispiel-Straße",
        "ort" : "Beispiel-Ort",
        "postleitzahl" : "12345",
        "land" : "Beispiel-Land",
        "telefon" : "1234567890",
        "mobil" : "9876543210",
        "email" : "[email protected]",
        "anrede" : "Herr",
        "vorname" : "Max",
        "nachname" : "Mustermann",
        "position" : "Beispiel-Position",
        "_id" : ObjectId("651cad07c493da4613ff123c"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "angebotseinstellungenDetails" : {
        "coupon_1" : {
            "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
            "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000"),
            "isActive" : true,
            "header" : "Beispiel-Header-1",
            "description" : "Beispiel-Beschreibung-1",
            "category" : ObjectId("64d172560b89315f7a336215"),
            "maximal_wert" : NumberInt(1),
            "_id" : ObjectId("651c93cb7d6aabe8e5abffc9")
        },
        "coupon_2" : {
            "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
            "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000"),
            "isActive" : true,
            "header" : "Beispiel-Header-2",
            "description" : "Beispiel-Beschreibung-2",
            "category" : ObjectId("64d1723b0b89315f7a333665"),
            "maximal_wert" : NumberInt(2),
            "_id" : ObjectId("651c93cb7d6aabe8e5abffca")
        },
        "zusatzvereinbarungen" : "Beispiel-Zusatzvereinbarungen",
        "_id" : ObjectId("651cad07c493da4613ff1236"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "beraterKundendatenDetails" : {
        "mitarbeiter_name" : "Max Mustermann",
        "mitarbeiter_nummer" : "6A4F8E1",
        "mitarbeiter_telefonist" : "0123456789",
        "ort_vertragsschliessung" : "Beispiel-Ort",
        "gesprochen_mit_anrede" : "Herr",
        "gesprochen_mit_vorname" : "Beispiel-Vorname",
        "gesprochen_mit_nachname" : "Beispiel-Nachname",
        "unterzeichnet_anrede" : "Herr",
        "unterzeichnet_position" : "Beispiel-Position",
        "unterzeichnet_vorname" : "Beispiel-Vorname",
        "unterzeichnet_nachname" : "Beispiel-Nachname",
        "_id" : ObjectId("651cad07c493da4613ff1239"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "textvorlageTeil1Details" : {
        "photosTakenByFieldSales" : "Nein",
        "customerProvideData" : "Keine Unterlagen",
        "customerDataSubmissionDate" : ISODate("2023-10-03T22:13:07.680+0000"),
        "_id" : ObjectId("651cad07c493da4613ff123d"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "unterschriftenDetails" : {
        "signatureLetters" : "Beispiel-Unterschriften-Letters",
        "signatureSign" : "Beispiel-Unterschriften-Sign",
        "_id" : ObjectId("651cad07c493da4613ff123e"),
        "createdAt" : ISODate("2023-10-04T00:08:39.228+0000"),
        "updatedAt" : ISODate("2023-10-04T00:08:39.228+0000")
    },
    "erfuellungsOrte" : [
        {
            "_id" : "651cafa16c1036d96b8196ae",
            "ausstattungsmerkmaleDetails" : {
                "accessible" : null,
                "accommodation" : null,
                "air_conditioning" : null,
                "apple_pay" : null,
                "beer_garden_terrace" : null,
                "bio" : null,
                "cocktail_menu" : null,
                "credit_card_payment" : null,
                "debit_card_payment" : null,
                "dogs_allowed" : null,
                "e_bike_rental" : null,
                "e_car_rental" : null,
                "event_hall" : null,
                "google_pay" : null,
                "halal" : null,
                "home_delivery" : null,
                "parking" : null,
                "party_service" : null,
                "paypal" : null,
                "playground_indoor" : null,
                "playground_outdoor" : null,
                "vegan" : null,
                "vegetarian" : null,
                "wifi" : null
            },
            "email" : "[email protected]",
            "entwertungs_email" : "[email protected]",
            "erfuellungsOrt" : "Beispiel-Ort-1",
            "facebook" : "Beispiel-Facebook-Link-1",
            "instagram" : "Beispiel-Instagram-Link-1",
            "land" : "Beispiel-Land",
            "location" : {
                "coordinates" : [
                    11.1941228,
                    47.6816119
                ],
                "type" : "Point"
            },
            "logoBilderDetails" : {
                "bilder" : [

                ],
                "logo" : "Beispiel-Logo-Link-1"
            },
            "mobil" : "9876543210",
            "oeffnungszeiten" : {
                "closedDays" : [

                ],
                "days" : [

                ],
                "extraOpeningHours" : ""
            },
            "ort" : "Beispiel-Ort-1",
            "postleitzahl" : "12345",
            "speisekarte" : "Beispiel-Speisekarte-Link-1",
            "strasse" : "Beispiel-Straße-1",
            "telefon" : "1234567890",
            "tiktok" : "Beispiel-TikTok-Link-1",
            "website" : "Beispiel-Website-Link-1",
            "whatsapp" : "9876543210"
        },
        {
            "_id" : "651cafa16c1036d96b8196be",
            "ausstattungsmerkmaleDetails" : {
                "accessible" : null,
                "accommodation" : null,
                "air_conditioning" : null,
                "apple_pay" : null,
                "beer_garden_terrace" : null,
                "bio" : null,
                "cocktail_menu" : null,
                "credit_card_payment" : null,
                "debit_card_payment" : null,
                "dogs_allowed" : null,
                "e_bike_rental" : null,
                "e_car_rental" : null,
                "event_hall" : null,
                "google_pay" : null,
                "halal" : null,
                "home_delivery" : null,
                "parking" : null,
                "party_service" : null,
                "paypal" : null,
                "playground_indoor" : null,
                "playground_outdoor" : null,
                "vegan" : null,
                "vegetarian" : null,
                "wifi" : null
            },
            "email" : "[email protected]",
            "entwertungs_email" : "[email protected]",
            "erfuellungsOrt" : "Beispiel-Ort-2",
            "facebook" : "Beispiel-Facebook-Link-2",
            "instagram" : "Beispiel-Instagram-Link-2",
            "land" : "Beispiel-Land",
            "location" : {
                "coordinates" : [
                    11.1941238,
                    47.6816139
                ],
                "type" : "Point"
            },
            "logoBilderDetails" : {
                "bilder" : [

                ],
                "logo" : "Beispiel-Logo-Link-2"
            },
            "mobil" : "9876543210",
            "oeffnungszeiten" : {
                "closedDays" : [

                ],
                "days" : [

                ],
                "extraOpeningHours" : ""
            },
            "ort" : "Beispiel-Ort-2",
            "postleitzahl" : "12345",
            "speisekarte" : "Beispiel-Speisekarte-Link-2",
            "strasse" : "Beispiel-Straße-2",
            "telefon" : "1234567890",
            "tiktok" : "Beispiel-TikTok-Link-2",
            "website" : "Beispiel-Website-Link-2",
            "whatsapp" : "9876543210"
        },
        {
            "_id" : "651cae939eb879dbc7f6ef74",
            "ausstattungsmerkmaleDetails" : {
                "accessible" : null,
                "accommodation" : null,
                "air_conditioning" : null,
                "apple_pay" : null,
                "beer_garden_terrace" : null,
                "bio" : null,
                "cocktail_menu" : null,
                "credit_card_payment" : null,
                "debit_card_payment" : null,
                "dogs_allowed" : null,
                "e_bike_rental" : null,
                "e_car_rental" : null,
                "event_hall" : null,
                "google_pay" : null,
                "halal" : null,
                "home_delivery" : null,
                "parking" : null,
                "party_service" : null,
                "paypal" : null,
                "playground_indoor" : null,
                "playground_outdoor" : null,
                "vegan" : null,
                "vegetarian" : null,
                "wifi" : null
            },
            "email" : "[email protected]",
            "entwertungs_email" : "[email protected]",
            "erfuellungsOrt" : "Beispiel-Ort-3",
            "facebook" : "Beispiel-Facebook-Link-3",
            "instagram" : "Beispiel-Instagram-Link-3",
            "land" : "Beispiel-Land",
            "location" : {
                "coordinates" : [
                    11.6541229,
                    48.0989907
                ],
                "type" : "Point"
            },
            "logoBilderDetails" : {
                "bilder" : [

                ],
                "logo" : "Beispiel-Logo-Link-3"
            },
            "mobil" : "9876543210",
            "oeffnungszeiten" : {
                "closedDays" : [

                ],
                "days" : [

                ],
                "extraOpeningHours" : ""
            },
            "ort" : "Beispiel-Ort-3",
            "postleitzahl" : "12345",
            "speisekarte" : "Beispiel-Speisekarte-Link-3",
            "strasse" : "Beispiel-Straße-3",
            "telefon" : "1234567890",
            "tiktok" : "Beispiel-TikTok-Link-3",
            "website" : "Beispiel-Website-Link-3",
            "whatsapp" : "9876543210"
        }
    ],
    "createdAt" : ISODate("2023-10-03T22:20:59.716+0000"),
    "updatedAt" : ISODate("2023-10-04T00:20:16.432+0000"),
    "__v" : NumberInt(0)
}

这是我的查询结果:

{
    "_id" : ObjectId("651c93cb7d6aabe8e5abffcf"),
    "kundennummer" : "5663732",
    "state" : "DRAFT",
    "erfuellungsorte" : [
        {
            "distance" : 22.35262369467075,
            "_id" : "651cafa16c1036d96b8196ae",
            "erfuellungsOrt" : "Beispiel-Ort-1",
            "facebook" : "Beispiel-Facebook-Link-1",
            "instagram" : "Beispiel-Instagram-Link-1",
            "tiktok" : "",
            "location" : {
                "coordinates" : [
                    11.1941228,
                    47.6816119
                ],
                "type" : "Point"
            },
            "logoBilderDetails" : {
                "bilder" : [

                ],
                "logo" : ""
            },
            "email" : "[email protected]",
            "entwertungs_email" : "[email protected]",
            "speisekarte" : "Beispiel-Speisekarte-Link-1",
            "oeffnungszeiten" : {
                "closedDays" : [

                ],
                "days" : [

                ],
                "extraOpeningHours" : ""
            },
            "website" : "Beispiel-Website-Link-1",
            "whatsapp" : "9876543210",
            "strasse" : "Beispiel-Straße-1",
            "ort" : "Beispiel-Ort-1",
            "land" : "Deutschland",
            "ausstattungsmerkmaleDetails" : {
                "accessible" : null,
                "accommodation" : null,
                "air_conditioning" : null,
                "apple_pay" : null,
                "beer_garden_terrace" : null,
                "bio" : null,
                "cocktail_menu" : null,
                "credit_card_payment" : null,
                "debit_card_payment" : null,
                "dogs_allowed" : null,
                "e_bike_rental" : null,
                "e_car_rental" : null,
                "event_hall" : null,
                "google_pay" : null,
                "halal" : null,
                "home_delivery" : null,
                "parking" : null,
                "party_service" : null,
                "paypal" : null,
                "playground_indoor" : null,
                "playground_outdoor" : null,
                "vegan" : null,
                "vegetarian" : null,
                "wifi" : null
            }
        }
    ]
}

别介意我替换的样本数据。我只是想象征只有一个履行地点回来,虽然前两个甚至几乎紧挨着对方!无论我指定哪个maxDistance,都只返回一个。如果半径为1,则没有结果。第一间约二十三公里。如果我设置1000公里,我只得到第一个回来(为什么?如果我跑了10公里,我什么也没得到(实际上是对的)。我真的绝望了!
非常感谢:)

8gsdolmq

8gsdolmq1#

您有erfuellungsOrte._id,但字段被定义为erfuellungsorte._id。MongoDB是区分大小写的。
如果你在一个空数组上执行$unwind,它将删除它。然后,即使它返回单独的文档,$first也会返回每个文档的第一个非空元素,这可能会因结果而异,而且它不会抛出错误。如果你确信$first会返回正确的字段,那就没问题。
无论哪种方式,您都可以在不使用$addfields的情况下执行此操作:

db.getCollection("companies").aggregate([
    {
        $geoNear: {
            near: {
                type: "Point",
                coordinates: [11.09576, 47.49209]
            },
            distanceField: "erfuellungsOrte.distance",
            maxDistance: 100000,
            spherical: true,
            distanceMultiplier: 0.001
        }
    },
    {
        $unwind: "$erfuellungsOrte"
    },
    {
        $group: {
            _id: "$_id",
            kundennummer: { $first: "$kundennummer" },
            logoBilderDetails: { $first: "$logoBilderDetails" },
            angebotseinstellungenDetails: { $first: "$angebotseinstellungenDetails" },
            state: { $first: "$state" },
            erfuellungsOrte: {
                $push: "$erfuellungsOrte"
            }
        }
    },
    {
        $project: {
            _id: 1,
            kundennummer: 1,
            state: 1,
            erfuellungsOrte: 1
        }
    }
]);

你也可以创建一个2dsphere索引,它将加快查询速度:

db.collection_name.createIndex({ "erfuellungsOrte.location": "2dsphere" });

PS你用AI创建了这个查询吗?

相关问题