MongoDB 3.4 - get array of subdocuments without root document
Try this:
db.collection.aggregate([ { $unwind: '$nodes' }, { $match: {_id: ObjectId("589eff3fee3d13019843f55a") } }, { $replaceRoot: { newRoot: "$nodes" } }]).toArray();
And you will get
[ { "_id" : ObjectId("589eff8f2bb59057c3f9b89d"), "name" : "Node A" }, { "_id" : ObjectId("589eff962bb59057c3f9b89e"), "name" : "Node B" }, { "_id" : ObjectId("589eff982bb59057c3f9b89f"), "name" : "Node C" }, { "_id" : ObjectId("589eff9a2bb59057c3f9b8a0"), "name" : "Node D" }]
$unwind operator is to deconstruct nodes
field from the input documents to output a document for each element.
And then use the $replaceRoot stage to promote the nodes
document to the top level, discarding the current top level fields.
Hope this helps.
Here is how you can do this with aggregation (Mongo v3.4+):
db.projects.aggregate([ {$match: {_id: ObjectId("589eff3fee3d13019843f55a")}}, {$unwind: '$nodes'}, {$replaceRoot: { newRoot: "$nodes"}}]).toArray();
Check out the docs.