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

188
Views
cómo obtener elementos de una matriz de objetos en mongodb

mi colección de db es como

 { "_id" : "57f4c1323eb5c694041ghea3", "example" : [ { "exId" : "57f4c0d43eb5c694041fdebd", "_id" : ObjectId("57f4c1323eb5c694041fded2"), "projects" : [ "57f4c1303eb5c694041fdec6" ] }, { "exId" : "57f24ee56da92f9c19b0efd4", "_id" : ObjectId("57f4cfa93eb5c694041fdf4c"), "projects" : [ "57f4cfa83eb5c694041fdf45" ] }, { "exId" : "57f24ec16da92f9c19b0efcd", "_id" : ObjectId("580864867a36f5bc1058c2c8"), "projects" : [ "580864837a36f5bc1058c2a9", "57f4c1303eb5c694041fdec6" ] } ],

Necesito obtener salida como

 "example": [ { "exId": "57f24ee56da92f9c19b0efd4", "_id": "57f4cfa93eb5c694041fdf4c", "projects": [ "57f4cfa83eb5c694041fdf45" ] }, { "exId" : "57f24ec16da92f9c19b0efcd", "_id" : ObjectId("580864867a36f5bc1058c2c8"), "projects" : [ "580864837a36f5bc1058c2a9", "57f4c1303eb5c694041fdec6" ] } ] }

donde esta mi consulta

 db.userDb.findOne({ '_id': req.params.userId }, { example: { $elemMatch: { exId: { $in: accIds } } } })

y accIds es una matriz que contiene valores "57f24ec16da92f9c19b0efcd", "57f24ee56da92f9c19b0efd4", "57f24fa26da92f9c19b0f005"

pero mi salida es

 "example": [ { "exId": "57f24ee56da92f9c19b0efd4", "_id": "57f4cfa93eb5c694041fdf4c", "projects": [ "57f4cfa83eb5c694041fdf45" ] }

],

cualquier ayuda es apreciada

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

db.collection.aggregate([ { $unwind: '$example' }, { $unwind: '$example.projects' }, { $match: { 'example.exId': { $in: accIds} } }, { $group: { _id: null, projectList: { $addToSet: '$example.projects' } } }, ])

Espero que esto funcione.

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!