I want to create a 2dsphere index on the "features" field in MongoDB. But it contains an array of GeoJSON LineStrings. Without this array, I could create 2dsphere index only for a single GeoJSON LineString like db.collection.createIndex({ loc : "2dsphere" ). I want to perform a spatial query like collection.find({ $features: { $geometry: bounding_box } })
Does anyone know how to create a 2dsphere for the array of GeoJSON objects?
{
"_id" : ObjectId("..."),
"name" : "street_segment_names",
"features" : [
{
"type" : "Feature",
"geometry" : {
"type" : "LineString",
"coordinates" : [
[.,.],[.,.],[.,.]
]
},
"properties" : {
"speed_limit" : 20
}
},
{
"type" : "Feature",
"geometry" : {
"type" : "LineString",
"coordinates" : [
[.,.],[.,.],[.,.]
]
},
"properties" : {
"speed_limit" : 30
}
}
]
}
MongoDB does not support "Feature" as a GeoJSON object type. See https://docs.mongodb.com/manual/reference/geojson/#geojson-objects
The structure shown in the question includes a "geometry" field in the features object that appears to be a supported object type.
You could create an index on that field using:
db.collection.createIndex({"features.geometry":"2dsphere"})
Depending on your goal with the "bounding box" query, you might use $geoWithin or $geoIntersects, like
db.collection.find({
"features.geometry":{
$geoWithin: {
$geometry: {
type: <"Polygon" or "MultiPolygon"> ,
coordinates: [ <coordinates> ]
}
}
}
})