案子
我已使用 2dsphere 索引将位置坐标存储在 MongoDB 集合中。我想获取包含在地图特定范围内的记录。我有一个固定的经纬度坐标点。我已经在 MongoDB 聚合中$geoWithin
使用了该方法。$box
我能够正确获取数据。
问题
我需要在我拥有的一个固定纬度和经度的每条记录中添加距离字段。
{
"$match": {
"location": {
"$geoWithin": {
"$box": [
[
0.024719919885622943,
51.54643953472475
],
[
-0.1589577534542408,
51.47239969138267
]
]
}
}
}
}
现在我有坐标数组[-0.0649729793707321, 51.50160291888072]
我需要从上述点开始的每条记录的距离。如何使用 MongoDB 聚合找到这个距离场?
数据库记录的示例文档
{
"_id" : ObjectId("5e342ac2e0dcfa65273751ea"),
"location" : {
"type" : "Point",
"coordinates" : [
-0.123507,
51.5083228
]
},
"status" : 1,
"price" : {
"value" : 27,
"type" : "day",
"base" : 3.38
},
"name" : "Initial Space with Wormup",
"venuePolicy" : "Venue Policies",
"updatedAt" : ISODate("2020-02-20T12:28:16.024Z"),
"createdAt" : ISODate("2020-01-31T13:25:22.600Z"),
"dimension" : {
"width" : 100
},
"description" : "<p>Hi, </p><p>please find attached document for testing purpose",
}