Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda