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

170
Views
¿Cómo obtener subdocumento sin documento raíz?

tengo este archivo json

 { "_id": "1", "name": "sanggiyo", "gender": "Male", "children": [ { "name": "tono", "gender": "Male", "children": [], "_id": "2" }, { "name": "rin", "gender": "Female", "children": [], "_id": "3" } ], "createdAt": "2022-04-17T11:14:00.648Z", "updatedAt": "2022-04-17T11:14:16.277Z", "__v": 0 }

Quiero obtener children._id usando

 Person.find( {$or: [ { "_id" : id }, { "children._id" : ObjectId(id) } ]} )

si uso la identificación de 1, debería obtener todos los documentos. pero si uso la identificación de 2, solo debería obtener ese objeto de niños sin la identificación 1, ¿hay alguna manera de hacerlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede intentar hacerlo con el marco de agregación, pero una forma mucho más legible y mantenible de hacerlo está en el código después de que la consulta de mongo arrojó algo similar a eso:

 const persons = await Person.find({$or: [ { "_id" : id }, { "children._id" : ObjectId(id) } ]}); const result = persons.flatMap(p => [p, ...p.children]).find(p => p._id === id);
about 4 years ago · Juan Pablo Isaza 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!