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

189
Vistas
How to get all keys of collection that contains certain(String) value in mongodb

Suppose I have a collection i.e. A.

I want to get all the keys to this collection that have particular value i.e. "hello"

   {
      "a": "its me hello",
      "b": "it does not have value",
      "c": "It has hello"
   }

In that case, I want to query to return a and c keys. Which contains the string "hello".

Is there any way to do that?

Or any way to do that in spring boot?

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

0

You can do this by reshaping the data in the datasource using objectToArray

Play

db.collection.aggregate([
  {
    "$project": {
      "data": {
        "$objectToArray": "$$ROOT"
      }
    }
  },
  {
    $unwind: "$data"
  },
  {
    "$match": {
      "data.v": {
        $regex: "hello"
      }
    }
  }
])

Another advanced version here It reshapes the data back

db.collection.aggregate([
  {
    "$project": {
      "data": {
        "$objectToArray": "$$ROOT"
      }
    }
  },
  {
    $unwind: "$data"
  },
  {
    "$match": {
      "data.v": {
        $regex: "hello"
      }
    }
  },
  {
    $group: {//Grouping back and restructuring the data so that objectToArray will bring the original format easily.
      "_id": "$_id",
      data: {
        "$addToSet": {
          k: "$data.k",
          v: "$data.v"
        }
      }
    }
  },
  {
    "$project": {
      "data": {
        "$arrayToObject": "$data"
      }
    }
  }
])

Refere the documentation of arrayToObject and objectToArray, then $regex

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