该 $geometry 运算符指定地理空间查询的 GeoJSON 几何对象。 它用于其他地理空间运算符来定义空间计算的形状和点。
语法
{
$geometry: {
type: <GeoJSON type>,
coordinates: <coordinates>
}
}
参数
| 参数 | DESCRIPTION |
|---|---|
type |
GeoJSON 对象类型(Point、Polygon、MultiPolygon 等) |
coordinates |
将 GeoJSON 对象定义为数组的坐标 |
例子
让我们了解 stores 数据集中的示例 json 的用法。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 1:查找指向几何图形的最接近的存储
为了获得更好的性能,请先创建所需的 2dsphere 索引。
db.stores.createIndex({ location: "2dsphere" })
查询最多检索两个距离坐标点最近的存储点 [46.2917, -62.6354],按邻近度排序。 它使用 $near 运算符按距离特定点对结果进行排序,帮助查找地理位置最接近给定位置的存储。
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [46.2917, -62.6354]
}
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查询返回的前两个结果为:
[
{
"_id": "59c355e9-586c-44f8-bbaf-a87989142119",
"name": "Relecloud | Outdoor Furniture Shop - Chetside",
"location": { "lat": 46.188, "lon": -62.2789 }
},
{
"_id": "d3a9cc23-e6ae-4806-93ac-1ade2f624742",
"name": "VanArsdel, Ltd. | Furniture Place - North Dustinside",
"location": { "lat": 47.3426, "lon": -62.4031 }
}
]
示例 2:查找多边形几何图形最接近的存储
此查询最多找到两个存储区,这些存储的位置与由 [-80.0, -75.0] 到 [-55.0, -70.0] 的坐标边界的已定义的矩形多边形相交。
$geoIntersects操作员查找与多边形边界重叠或触摸的存储 - 非常适合用于识别与特定地理区域交互的位置,无论是完全位于其中还是只是越过边缘。
db.stores.find({
location: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [[
[-80.0, -75.0], // Bottom-left
[-80.0, -70.0], // Top-left
[-55.0, -70.0], // Top-right
[-55.0, -75.0], // Bottom-right
[-80.0, -75.0] // Close polygon
]]
}
}
}
}, {
name: 1,
location: 1,
city: 1
}).limit(2)
此查询返回的前两个结果。
[
{
"_id": "6bba7117-d180-4584-b50c-a2f843e9c9ab",
"name": "Wide World Importers | Craft Supply Mart - Heaneybury",
"location": { "lat": -64.4843, "lon": -107.7003 },
"city": "Heaneybury"
},
{
"_id": "2fd37663-e0ff-41d0-9c5a-3aec86285daa",
"name": "Relecloud | Cleaning Supply Closet - Patiencehaven",
"location": { "lat": -70.6077, "lon": -105.9901 },
"city": "Patiencehaven"
}
]
示例 3:查找与多多边形几何图形最近的存储
该示例最多检索两个存储区的位置位于两个定义的矩形区域(MultiPolygon):一个位于坐标 [120.0, -13.0] 附近的 [125.0, -10.0] 和另一个靠近 [44.0, -64.0] 到 [48.0, -61.0]。
它使用具有 MultiPolygon 几何图形的 $geoWithin 运算符搜索由任何指定多边形括起来的存储区,使其可用于同时跨多个非相邻地理区域进行查询。
db.stores.find({
location: {
$geoWithin: {
$geometry: {
type: "MultiPolygon",
coordinates: [
[[
[120.0, -13.0],
[120.0, -10.0],
[125.0, -10.0],
[125.0, -13.0],
[120.0, -13.0]
]],
[[
[44.0, -64.0],
[44.0, -61.0],
[48.0, -61.0],
[48.0, -64.0],
[44.0, -64.0]
]]
]
}
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查询返回的前两个结果为:
[
{
"_id": "6d70de9c-7b83-426d-81aa-f2173f97b64d",
"name": "Fabrikam, Inc. | Footwear Haven - Port Erling",
"location": { "lat": 45.641, "lon": -118.4963 }
},
{
"_id": "96d48224-ce10-4a61-999a-8536d442f81a",
"name": "Wide World Importers | Eyewear Bazaar - West Oletachester",
"location": { "lat": 47.3461, "lon": -61.6605 }
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。