对于在美国发布的电影,其tomato.viewer.rating大于或等于3,请计算一个名为num_favs的新字段,该字段表示在电影的投射字段中显示的收藏数量。
我不确定如何继续进一步请你帮忙
favorites =
["Sandra Bullock",
"Tom Hanks",
"Julia Roberts",
"Kevin Spacey",
"George Clooney"]
db.movies.aggregate([
{ $match:{
'tomatoes.viewer.rating':{$gte:3},
"countries":"USA"
}
}
]).
答案 0 :(得分:2)
var favorites = [
"Sandra Bullock",
"Tom Hanks",
"Julia Roberts",
"Kevin Spacey",
"George Clooney"]
db.movies.aggregate([
{
$match:
{
"tomatoes.viewer.rating": { $gte:3 },
"cast":
{
$in:["Sandra Bullock","Tom Hanks","Julia Roberts","Kevin Spacey","George Clooney"]
}
}
},
{
$project:
{
_id:0,
"tomatoes.viewer.rating":1,
title:1,
cast:1,
num_favs:
{
$size:
{
$setIntersection:["$cast",favorites]
}
}
}
},
{
$sort:
{
"num_favs":-1,
"tomatoes.viewer.rating":-1,
title:-1
}
}
])
我认为这可能会给你正确答案。我发现如果你不匹配演员阵容那么你在num_favs中得到空白或null,但如果你匹配演员阵容,那么你得到正确的答案。如果有任何变化,请告诉我。谢谢。