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.
field array,field object to root{ $unwind: "$field" },
{ $replaceRoot: { newRoot: "$field" } }
For unique object try $group stage after $unwind stage,
{
$group: {
_id: "$field.name",
field: { $first: "$field" }
}
},