I have a document similar to the following, where I want to return a field of the current top level documents as the top level document itself in the results array:
{ field1:{contents:{}} field2:{othercontent:{}} }
I want the results of my aggregation query to return the following
{ contents:{} }
Can this be done with $project and the aggregation framework?
Yes, you can use $project
to do that. You just have to tell it to retrieve the nested contents
object using dot notation:
db.items.aggregate( {$project: {contents:'$field1.contents'}} );
Additionally, if you want to to hide the _id
field from the output, you can specify _id: 0
in the $project
parameters:
db.items.aggregate( {$project: {contents:'$field1.contents', _id:0}} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With