Saya pikir yang Anda inginkan adalah ini:
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"]
Outputnya (selain spasi) adalah:
[ { u'episodes': [ { u'title': u'Episode 1',
u'desc': u'...'
}
],
u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
u'title': u'The Hitchhikers Guide to the Galaxy'
}
]
Jika Anda ingin menyingkirkan u"_id"
, tambahkan pipeline dengan:
{"$project": {"_id": 0,
"episodes": "$episodes",
"title": "$title"}
}