程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Mongodb 查询以使用 split 根据电影的导演数量显示电影数量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Mongodb 查询以使用 split 根据电影的导演数量显示电影数量?

开发过程中遇到Mongodb 查询以使用 split 根据电影的导演数量显示电影数量的问题如何解决?下面主要结合日常开发的经验,给出你关于Mongodb 查询以使用 split 根据电影的导演数量显示电影数量的解决方法建议,希望对你解决Mongodb 查询以使用 split 根据电影的导演数量显示电影数量有所启发或帮助;

我有一个 movIEDetails.Json 数据库,集合名称是电影,然后我们的老师希望我们根据电影拥有的导演数量显示电影数量 一种。提示您可能必须使用 JavaScript 函数 split 由于导演不是一个数组,它只是一个字符串,所以我无法计算有多少个导演,因为它总是以 1 的形式出现,因为它是一个字符串,所以我想拆分该字符串以便可以获得一个数组这样我就可以数了。但不幸的是,我不知道可以解决这个问题的查询。 前任。预期输出:

@H_301_3@    {_ID:1,value: 100}
    {_ID:2,value: 200}  etc.

_ID 是文档拥有的董事人数

@H_301_3@    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
    }
  }
])
@H_675_31@

较短的版本

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

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

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

如果要计算董事为 0 的记录

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

大佬总结

以上是大佬教程为你收集整理的Mongodb 查询以使用 split 根据电影的导演数量显示电影数量全部内容,希望文章能够帮你解决Mongodb 查询以使用 split 根据电影的导演数量显示电影数量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。