Jeg tror, det du ønskede, er dette:
print db.dvds.aggregate([
{"$unwind": "$episodes"}, # One document per episode
{"$match": {"episodes.title": "Episode 1"} }, # Selects (filters)
{"$group": {"_id": "$_id", # Put documents together again
"episodes": {"$push": "$episodes"},
"title": {"$first": "$title"} # Just take any title
}
},
])["result"]
Outputtet (udover mellemrummene) er:
[ { u'episodes': [ { u'title': u'Episode 1',
u'desc': u'...'
}
],
u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
u'title': u'The Hitchhikers Guide to the Galaxy'
}
]
Hvis du vil slippe af med u"_id"
, tilføj pipelinen med:
{"$project": {"_id": 0,
"episodes": "$episodes",
"title": "$title"}
}