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

117
Views
¿Cómo obtener todos los elementos coincidentes de una matriz de objetos en MongoDB?

Tengo un documento mongo como el siguiente.

 { "_id" : ObjectId("588adde40fcbbbc341b34e1c"), "title" : "Fifa world cup", "tags" : [ { "name" : "Football", "type" : "Sports" }, { "name" : "World cup", "type" : "Sports" }, { "name" : "Fifa", "type" : "Manager" } ] }

Escribí la siguiente consulta para obtener todas las etiquetas con el tipo Sports , pero solo obtengo 1 elemento en lugar de 2

 db.collection.find( { tags: { $elemMatch: { type: "Sports" } } }, { "tags.$" : 1 })

¿Es posible obtener todos los elementos coincidentes? ¿Qué me estoy perdiendo aquí?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes usar la agregación:

 db.collection.aggregate([ { $unwind : "$tags" }, { $match : { "tags.type" : "Sports" } }, { $group : { _id : "$_id", tags : {$addToSet : "$tags"} } } ])
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!