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

154
Vistas
Querying keys in a docs on mongoDB by prefix

At the moment, i have this data in mongo

{
   "_id" : ObjectId("62021f4cbd7796d438622245"),
    "id" : "MyData",
    "Mode" : "txtx",
    "MinCost" : "0.2",
    "R:1" : "info1",
    "R:13" : "info2",
    "R:3" : "info3",
    "R:4 : "info4",
    "R:5" : "info5",
    "Cost" : "100"
}

I want to return back just the keys from this data that has prefix, for example, "R" or "R:1". Basically, want to return just the keys that matches a prefix that i'm giving.

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

0

  • $expr
  • $gte
  • $size
  • $filter
  • $replaceRoot
  • $arrayToObject
  • $objectToArray
  • $regexFind
db.collection.aggregate([
  {
    "$match": {
      $expr: {
        "$gte": [
          {
            $size: {
              "$filter": {
                "input": { "$objectToArray": "$$ROOT"},
                "as": "s",
                "cond": { "$regexFind": { "input": "$$s.k", "regex": "R:1" } }
              }
            }
          },
          1
        ]
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$arrayToObject": {
          "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "as": "s",
            "cond": { "$regexFind": { "input": "$$s.k", "regex": "R:1" } }
          }
        }
      }
    }
  }
])

mongoplayground

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