运算符 $geoWithin 选择其位置字段完全位于指定几何图形中的文档。 此运算符支持各种形状运算符,包括$box、$polygon$center和$geometry。
语法
// Using $box
{
<location field>: {
$geoWithin: {
$box: [ [ <bottom left coordinates> ], [ <upper right coordinates> ] ]
}
}
}
// Using $center
{
<location field>: {
$geoWithin: {
$center: [ [ <x>, <y> ], <radius> ]
}
}
}
// Using $geometry
{
<location field>: {
$geoWithin: {
$geometry: {
type: <GeoJSON type>,
coordinates: <coordinates>
}
}
}
}
参数
| 参数 | DESCRIPTION |
|---|---|
location field |
包含位置坐标的字段 |
$box |
定义框对面角的两组坐标 |
$center |
中心点坐标和半径(以度为单位) |
$geometry |
定义边界的 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:查找由$box定义的存储
为了获得更好的性能,请确保具有 2dsphere 索引。
db.stores.createIndex({ location: "2dsphere" })
此查询查找位于地图上特定矩形区域的存储区(由框(边界矩形)定义。
db.stores.find({
location: {
$geoWithin: {
$box: [
[65.0, 65.0], // Bottom left corner
[75.0, 75.0] // Top right corner
]
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查询返回的前两个结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"location": {
"lat": 70.1272,
"lon": 69.7296,
"address": "123 Entertainment Blvd",
"city": "East Linwoodbury"
}
},
{
"_id": "fc286536-cb94-45aa-b975-7040fde04cf7",
"name": "First Up Consultants | Medical Supply Corner - South Elnoraview",
"location": {
"lat": 72.2184,
"lon": 68.9829
}
}
]
示例 2:查找由$center定义的存储
查询使用 $geoWithin 运算符查找由中心点和半径定义的圆形区域内的存储。
db.stores.find({
'location': {
$geoWithin: {
$center: [
[-82.5543, -65.105], // Center point (Wide World Importers location)
5 // Radius in degrees
]
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查询返回的前两个结果为:
[
{
"_id": "3e962dd0-dffb-49d6-8a96-1d29fa1553d2",
"name": "Tailwind Traders | Book Center - Lake Marlen",
"location": { "lat": -85.4034, "lon": -65.9189 }
},
{
"_id": "7e442816-be4c-4919-8f67-d1e9162a511f",
"name": "Proseware, Inc. | Outdoor Furniture Bargains - North Obieberg",
"location": { "lat": -84.1013, "lon": -69.5717 }
}
]
示例 3:查找由$geometry定义的存储
此查询最多找到两个存储区,其位置位于定义的矩形多边形中。
db.stores.find({
'location': {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [[
[-85.0, -70.0],
[-85.0, -60.0],
[-75.0, -60.0],
[-75.0, -70.0],
[-85.0, -70.0]
]]
}
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查询返回的前两个结果为:
[
{
"_id": "66fd4cdd-ffc3-44b6-81d9-6d5e9c1f7f9a",
"name": "Trey Research | Health Food Center - North Michelle",
"location": { "lat": -77.9951, "lon": -62.7339 }
},
{
"_id": "ea3f775b-f977-4827-ada4-ca7fd8ed0cd4",
"name": "VanArsdel, Ltd. | Outdoor Equipment Pantry - Port Aleenton",
"location": { "lat": -76.4516, "lon": -67.2051 }
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。