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

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

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 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