I have a collection like this:
{ \"_id\" : ObjectId(\"5bd1686ba64b9206349284db\"), \"type\" : \"Package\", \"codeInstances\" : [ {
You can try below aggregation using $replaceRoot
db.collection.aggregate([ { "$match": { "codeInstances.name": "b", "type": "Package" }}, { "$unwind": "$codeInstances" }, { "$match": { "codeInstances.name": "b", "type": "Package" }}, { "$replaceRoot": { "newRoot": "$codeInstances" }} ])