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

131
Views
Get single array from array of array in mongoose aggregate pipeline

I have a collection like this (That I got from last pipeline condition):

[
{_id:"1","field":[{...obj1},{...obj2}]},
{_id:"2","field":[{...obj3},{...obj1}]},
{_id:"3","field":[{...obj4},{...obj5}]},
{_id:"4","field":[{...obj1},{...obj2}]},
]

I want to add more conditions in the pipeline to get :

[{...obj1},{...obj2},{...obj3},{...obj4},{...obj5}]

The order doesn't matter but I want all the objects from field. I am new to mongo and mongoose, so I don't know which aggregation property will work here.

Please help me, I think it is a simple task but I am unable to do so because of lack of knowledge.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  • $unwind to deconstruct field array,
  • $replaceRoot to replace field object to root
{ $unwind: "$field" },
{ $replaceRoot: { newRoot: "$field" } }

Playground


For unique object try $group stage after $unwind stage,

  {
    $group: {
      _id: "$field.name",
      field: { $first: "$field" }
    }
  },

Playground

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!