Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
Wildcard for key in mongodb query

I have a collection equivalent to:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "sides": {
      "0": {
        "dist": 100
      },
      "1": {
        "dist": 10
      }
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "sides": {
      "0": {
        "dist": 100
      }
    }
  }
]

I would like to perform a query that return any documents that has for any key nested in sides has the key dist with a specific value. Something like:

db.collection.find({"sides.*.dist": 10})

Here * acts as a wildcard, any key would be valid in its place.

That would retrieve:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "sides": {
      "0": {
        "dist": 100
      },
      "1": {
        "dist": 10
      }
    }
  }
]

On the other hand

db.collection.find({"sides.*.dist": 100})

Would retrive both documents.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

the following song and dance won't be neccessary if sides field was an array...

db.collection.find(
{
    $expr: {
        $gt: [{
            $size: {
                $filter: {
                    input: { $objectToArray: "$sides" },
                    as: "x",
                    cond: { $eq: ["$$x.v.dist", 10] }
                }
            }
        }, 0]
    }
})
over 4 years ago · Santiago Trujillo Denunciar

0

You could get the matching elements using this

db.collection.aggregate([
  {
    "$project": {
      "sides_array": {//Reshape the sides
        "$objectToArray": "$sides"
      }
    }
  },
  {//Denormalize to get more than one matches
    "$unwind": "$sides_array"
  },
  {
    "$match": {//Condition goes here
      "sides_array.v.dist": 10
    }
  },
  {
    "$group": {//Group the data back, after unwinding
      "_id": "$_id",
      "sides": {
        "$push": "$sides_array"
      }
    }
  },
  {
    "$project": {//Reshape the data
      "_id": 1,
      "sides": {
        "$arrayToObject": "$sides"
      }
    }
  }
])
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda