I would like to output result of my $unwind operation into another collection and getting duplicate _id error. In order to overcome it I am using $group to produce a new _id field. How can I project extra fields from collection? When I am using $project clause it doesn't output anything.
This one works:
db.audit.aggregate([
{$match: { "date": { $gte : ISODate("2015-01-30T00:00:00.000Z"),
$lt : ISODate("2015-01-31T00:00:00.000Z")
}
}
},
{ $unwind : "$data.items" } ,
{ $group : {
"_id" : {
"_id":"$_id",
"sku":"$data.items.sku"
}
}
},
{
$out : "randomAggregates"
}
]
)
This one doesn't:
db.audit.aggregate([
{$match: { "date": { $gte : ISODate("2015-01-30T00:00:00.000Z"),
$lt : ISODate("2015-01-31T00:00:00.000Z")
}
}
},
{ $unwind : "$data.items" } ,
{ $project: { "ccy" : "$data.crncy",
"coo" : "$data.items.coo",
"order_id" : "$meta.refid"
}
},
{ $group : {
"_id" : {
"_id":"$_id",
"sku":"$data.items.sku"
}
}
},
{
$out : "randomAggregates"
}
]
)
Thank you