Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
MongoDB create 2dsphere indexes with array of features for spatial query

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
            }
        }
    ]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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> ]
        }
     }
  }
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!