Ik denk dat je dit wilde:
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"]
De uitvoer (naast de spaties) is:
[ { u'episodes': [ { u'title': u'Episode 1',
u'desc': u'...'
}
],
u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
u'title': u'The Hitchhikers Guide to the Galaxy'
}
]
Als u zich wilt ontdoen van de u"_id"
, voeg de pijplijn toe met:
{"$project": {"_id": 0,
"episodes": "$episodes",
"title": "$title"}
}