Return whole document from aggregation
Currently you cannot get the whole comment
document via single $first
operator. But you can include other necessary fields (similar to _id
field) during $group
step:
{
"$group": {
_id: "$post_id",
lastComment: { "$first": "$_id" },
field_1: { "$first": "$field_1" },
field_2: { "$first": "$field_2" },
// ...
field_N: { "$first": "$field_N" }
}
}
According to this JIRA ticket: https://jira.mongodb.org/browse/SERVER-5916, the whole document will be available to return from aggregation operations from 2.5.3 version. It will be possible using new variables: $$ROOT
or $$CURRENT
:
{
"$group": {
_id: "$post_id",
lastComment: { "$first": "$$CURRENT" }
}
}