Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Cómo obtener todas las claves de colección que contienen cierto valor (Cadena) en mongodb

Supongamos que tengo una colección, es decir, A.

Quiero obtener todas las claves de esta colección que tengan un valor particular, es decir, "hola".

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

En ese caso, quiero consultar para devolver las claves a y c . Que contiene la cadena "hola".

¿Hay alguna manera de hacerlo?

¿O alguna forma de hacerlo en Spring Boot?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede hacer esto remodelando los datos en la fuente de datos usando objectToArray

Jugar

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

Otra versión avanzada aquí Le da nueva forma a los datos.

 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" } } } ])

Consulte la documentación de arrayToObject y objectToArray , luego $regex

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!