在子字段中创建多边形的 2dsphere 索引
Create 2dsphere index of polygon in subfield
我在 MongoDB 3.6 中创建 2dsphere 索引时遇到问题。我在 GeoJSON 中有一系列多边形。多边形之一是以下示例(简化为仅 4 个点):
GeoJSON
{
"_id" : ObjectId("5a92b5ad370dfa460e07f2ab"),
"type" : "FeatureCollection",
"crs" : {
"type" : "name",
"properties" : {
"name" : "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features" : [
{
"type" : "Feature",
"properties" : {
"ManCatID" : NumberInt(3075),
"ManCatName" : "Field1"
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [
[
[
-2.590805067250554,
52.57471588485983
],
[
-2.594339050478125,
52.57415879313657
],
[
-2.590791776038,
52.573727037479124
],
[
-2.590805067250554,
52.57471588485983
]
]
]
}
}
]
}
当尝试按照 official website example 中的说明创建 2dsphere 索引时,我需要使用子字段 'features.geometry.coordinates'。但是,当我尝试使用 db.CaseStudies.createIndex({ features.geometry: "2dsphere" });
.
创建时它失败了
如果我使用db.CaseStudies.createIndex({ geometry: "2dsphere" })
,我不会出错。但是,当我进行查询时,我总是获得 'null' 个结果。我使用的查询是:
db.CaseStudies.findOne({ geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ -2.592, 52.574 ] } } } });
有谁知道我做错了什么吗?
您可以尝试改用此查询吗?:
db.CaseStudies.findOne({
"features.geometry": {
$geoIntersects: { $geometry: { type: "Point", coordinates: [ -2.592, 52.574 ] } }
}
});
您匹配的字段应该是 features.geometry
而不是 geometry
。
关于创建索引失败的问题,能不能在features.geometry
:
两边加上引号试试
db.CaseStudies.createIndex({ "features.geometry": "2dsphere" });
我在 MongoDB 3.6 中创建 2dsphere 索引时遇到问题。我在 GeoJSON 中有一系列多边形。多边形之一是以下示例(简化为仅 4 个点):
GeoJSON
{
"_id" : ObjectId("5a92b5ad370dfa460e07f2ab"),
"type" : "FeatureCollection",
"crs" : {
"type" : "name",
"properties" : {
"name" : "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features" : [
{
"type" : "Feature",
"properties" : {
"ManCatID" : NumberInt(3075),
"ManCatName" : "Field1"
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [
[
[
-2.590805067250554,
52.57471588485983
],
[
-2.594339050478125,
52.57415879313657
],
[
-2.590791776038,
52.573727037479124
],
[
-2.590805067250554,
52.57471588485983
]
]
]
}
}
]
}
当尝试按照 official website example 中的说明创建 2dsphere 索引时,我需要使用子字段 'features.geometry.coordinates'。但是,当我尝试使用 db.CaseStudies.createIndex({ features.geometry: "2dsphere" });
.
如果我使用db.CaseStudies.createIndex({ geometry: "2dsphere" })
,我不会出错。但是,当我进行查询时,我总是获得 'null' 个结果。我使用的查询是:
db.CaseStudies.findOne({ geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ -2.592, 52.574 ] } } } });
有谁知道我做错了什么吗?
您可以尝试改用此查询吗?:
db.CaseStudies.findOne({
"features.geometry": {
$geoIntersects: { $geometry: { type: "Point", coordinates: [ -2.592, 52.574 ] } }
}
});
您匹配的字段应该是 features.geometry
而不是 geometry
。
关于创建索引失败的问题,能不能在features.geometry
:
db.CaseStudies.createIndex({ "features.geometry": "2dsphere" });