我一直在应用程序中使用基于位置的搜索功能,并且设置了 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" : "beispiel[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,则不会得到任何结果。第一个距离约23公里。如果我设置 1000 公里,我只能返回第一个(为什么???)。如果我跑了 10 公里,我就没有任何回报(实际上是正确的)。我真的快绝望了!
非常感谢:)
您有
$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 创建的吗?