Mongodb查询显示基于使用拆分的电影导演数量的电影数量



我有一个movieDetails.json数据库,集合名称是一部电影,然后我们的老师希望我们根据一部电影的导演数量显示电影数量a.提示您可能必须使用javascript函数split由于director不是数组,它只是一个字符串,所以我无法计算有多少个director,因为它是字符串,所以它总是显示为1,所以我想拆分这个字符串,这样我就可以得到一个数组,这样我就能计算它。但不幸的是,我不知道能解决这个问题的查询。预期输出示例:

{_id:1 , value: 100}
{_id:2 , value: 200}  etc.

_id是文件具有的董事人数

db.movie.find().pretty()
{
"_id" : ObjectId("5b107bec1d2952d0da9046e1"),
"title" : "A Million Ways to Die in the West",
"year" : 2014,
"rated" : "R",
"runtime" : 116,
"countries" : [
"USA"
],
"genres" : [
"Comedy",
"Western"
],
"director" : "Seth MacFarlane",
"writers" : [
"Seth MacFarlane",
"Alec Sulkin",
"Wellesley Wild"
],
"actors" : [
"Seth MacFarlane",
"Charlize Theron",
"Amanda Seyfried",
"Liam Neeson"
],
"plot" : "As a cowardly farmer begins to fall for the mysterious new woman in town, he must put his new-found courage to the test when her husband, a notorious gun-slinger, announces his arrival.",
"poster" : "http://ia.media-imdb.com/images/M/MV5BMTQ0NDcyNjg0MV5BMl5BanBnXkFtZTgwMzk4NTA4MTE@._V1_SX300.jpg",
"imdb" : {
"id" : "tt2557490",
"rating" : 6.1,
"votes" : 126592
},
"tomato" : {
"meter" : 33,
"image" : "rotten",
"rating" : 4.9,
"reviews" : 188,
"fresh" : 62,
"consensus" : "While it offers a few laughs and boasts a talented cast, Seth MacFarlane's overlong, aimless A Million Ways to Die in the West is a disappointingly scattershot affair.",
"userMeter" : 40,
"userRating" : 3,
"userReviews" : 62945
},
"metacritic" : 44,
"awards" : {
"wins" : 0,
"nominations" : 6,
"text" : "6 nominations."
},
"type" : "movie"
}
{
"_id" : ObjectId("5b107bec1d2952d0da9046e3"),
"title" : "West Side Story",
"year" : 1961,
"rated" : "UNRATED",
"runtime" : 152,
"countries" : [
"USA"
],
"genres" : [
"Crime",
"Drama",
"Musical"
],
"director" : "Jerome Robbins, Robert Wise",
"writers" : [
"Ernest Lehman",
"Arthur Laurents",
"Jerome Robbins"
],
"actors" : [
"Natalie Wood",
"Richard Beymer",
"Russ Tamblyn",
"Rita Moreno"
],
"plot": "Two youngsters from rival New York City gangs fall in love, but tensions between their respective friends build toward tragedy.",
"poster" : "http://ia.media-imdb.com/images/M/MV5BMTM0NDAxOTI5MF5BMl5BanBnXkFtZTcwNjI4Mjg3NA@@._V1_SX300.jpg",
"imdb" : {
"id" : "tt0055614",
"rating" : 7.6,
"votes" : 67824
},
"awards" : {
"wins" : 18,
"nominations" : 11,
"text" : "Won 10 Oscars. Another 18 wins & 11 nominations."
},
"type" : "movie"
}
"director" : "Sergio Leone",
"writers" : [
"Sergio Donati",
"Sergio Leone",
"Dario Argento",
"Bernardo Bertolucci",
"Sergio Leone"
],
"actors" : [
"Claudia Cardinale",
"Henry Fonda",
"Jason Robards",
"Charles Bronson"
],
"plot": "Epic story of a mysterious stranger with a harmonica who joins forces with a notorious desperado to protect a beautiful widow from a ruthless assassin working for the railroad.",
"poster" : "http://ia.media-imdb.com/images/M/MV5BMTEyODQzNDkzNjVeQTJeQWpwZ15BbWU4MDgyODk1NDEx._V1_SX300.jpg",
"imdb" : {
"id" : "tt0064116",
"rating" : 8.6,
"votes" : 201283
},
"tomato" : {
"meter" : 98,
"image" : "certified",
"rating" : 9,
"reviews" : 54,
"fresh" : 53,
"consensus" : "A landmark Sergio Leone spaghetti western masterpiece featuring a classic Morricone score.",
"userMeter" : 95,
"userRating" : 4.3,
"userReviews" : 64006
},
"metacritic" : 80,
"awards" : {
"wins" : 4,
"nominations" : 5,
"text" : "4 wins & 5 nominations."
},
"type" : "movie"
}


演示-https://mongoplayground.net/p/y3kvFnocWKn

使用聚合查询

阅读以下链接以更好地理解

$set

$group

$split

$size

db.movie.aggregate([
{
$match: { director: { $ne: null } }
},
{
$set: { // set directorsCount
directorsCount: {
$size: { // get the size of the array
$split: ["$director", ","] // split by comma will return array
}
}
}
},
{
$group: {
_id: "$directorsCount", // group by directorsCount
value: { $sum: 1 } // count
}
}
])

较短版本

演示-https://mongoplayground.net/p/Nt-NDBpN4Ad

db.movie.aggregate([
{
$match: { director: { $ne: null } }
},
{
$group: {
_id: { $size: { $split: [ "$director", "," ] } },
value: { $sum: 1 }
}
}
])

演示-https://mongoplayground.net/p/f8fuZVIjc-_

如果您想计算控制器为0 的记录

db.movie.aggregate([
{
$group: {
_id: {
$size: {
$ifNull: [
{ $split: [ "$director", "," ] },
[]
]
}
},
value: { $sum: 1 }
}
}
])

最新更新