我们会在find()命令中使用什么查询文档来返回video.movieDetails集合中所有获得或被提名以获得最佳图片的电影?您可以假设只有在电影获胜或被提名时,奖项才会出现在“奥斯卡”阵列中。
"awards" : {
"oscars" : [
{"award": "bestAnimatedFeature", "result": "won"},
{"award": "bestMusic", "result": "won"},
{"award": "bestPicture", "result": "nominated"},
{"award": "bestSoundEditing", "result": "nominated"},
{"award": "bestScreenplay", "result": "nominated"}
],
"wins" : 56,
"nominations" : 86,
"text" : "Won 2 Oscars. Another 56 wins and 86 nominations."
}
答案 0 :(得分:0)
您的意思是这个查询吗?
db.movie.find({"awards.oscars.award":"bestPicture"})
输入:
{
"_id" : ObjectId("569a7f590586bcb40f7d2532"),
"awards" : {
"oscars" : [
{
"award" : "bestAnimatedFeature",
"result" : "won"
},
{
"award" : "bestMusic",
"result" : "won"
},
{
"award" : "bestPicture",
"result" : "nominated"
},
{
"award" : "bestSoundEditing",
"result" : "nominated"
},
{
"award" : "bestScreenplay",
"result" : "nominated"
}
],
"wins" : 56,
"nominations" : 86,
"text" : "Won 2 Oscars. Another 56 wins and 86 nominations."
}
}
{
"_id" : ObjectId("569a7fc60586bcb40f7d2533"),
"awards" : {
"oscars" : [
{
"award" : "bestScreenplay",
"result" : "nominated"
}
],
"wins" : 12,
"nominations" : 15,
"text" : "Won 1 Oscars. Another 12 wins and 15 nominations."
}
}
结果
{
"_id" : ObjectId("569a7f590586bcb40f7d2532"),
"awards" : {
"oscars" : [
{
"award" : "bestAnimatedFeature",
"result" : "won"
},
{
"award" : "bestMusic",
"result" : "won"
},
{
"award" : "bestPicture",
"result" : "nominated"
},
{
"award" : "bestSoundEditing",
"result" : "nominated"
},
{
"award" : "bestScreenplay",
"result" : "nominated"
}
],
"wins" : 56.0000000000000000,
"nominations" : 86.0000000000000000,
"text" : "Won 2 Oscars. Another 56 wins and 86 nominations."
}
}
答案 1 :(得分:0)
如果我们可以假设bestPicture位于第2位,那么我们可以执行以下
{
"$or": [{
"awards.oscars.2.result": "won"
},
{
"awards.oscars.2.result": "nominated"
}]
})
如果我们想确保胜利和提名确实存在,那么我们可能应该运行
{
"$and": [{
"$or": [{
"awards.wins": {
"$gt": 0
}
},
{
"awards.nominations": {
"$gt": 0
}
}]
},
{
"$or": [{
"awards.oscars.2.result": "won"
},
{
"awards.oscars.2.result": "nominated"
}]
}]
}
或者我们可以使用elemMatch
{
"awards.oscars": {
"$elemMatch": {
"$or": [{
"award": "bestPicture",
"result": "won"
},
{
"award": "bestPicture",
"result": "nominated"
}]
}
}
}