【问题标题】:MongoDB, counting the occurrences of EACH item in several arraysMongoDB,计算几个数组中每个项目的出现次数
【发布时间】:2021-05-21 14:13:35
【问题描述】:

我对 Mongo 还很陌生,如果这有一些我没有掌握的简单答案,我很抱歉。

我希望能够计算每个数字(流派 ID)出现在所有“movie_genres”数组中的次数,并获取每个数字的计数。我的最终目标是统计某个类型编号在用户帐户中出现的次数(包括所有电影)。

我希望找回这个用户有 2 个28,2 个53,1 个18

电影可以一直添加到用户的“movies_watched”数组中。

const userSeed = [
  {
    email: "rambo@hotmail.com",
    password: "12345",
    movies_watched: [
      {
        title: "Rambo",
        movie_id: 7555,
        movie_runtime: 99,
        movie_genres: [28, 53],
      },
      {
        title: "Rambo: Last Blood",
        movie_id: 522938,
        movie_runtime: 99,
        movie_genres: [28, 53, 18],
      },
    ],

【问题讨论】:

    标签: javascript arrays mongodb mongodb-query mongoose-schema


    【解决方案1】:

    您需要使用聚合和设置(仅唯一值)

    查询:

    db.getCollection('movies')
        .aggregate([
            // optionaly filter records to apply on
            //{
            //    $match: { email: /rambo/ }
            //},
            {
                $project: { // project values you want to see in result
                    // record id is present by default
                    email: "$email",
                    genres: { // brand new field with desired output
                        $reduce: { // reduce like Array.reduce in javascript
                            input: "$movies_watched", // array to reduce
                            initialValue: [],
                            in: {
                                $setUnion: // the function doing the magic
                                // if you use $concatArrays instead - it will contain duplicities
                                [
                                    "$$value", // destination in initialValue
                                    "$$this.movie_genres" // field to take items from
                                ]
                            }
                        }
                    }
                }
            }
        ])
    

    结果:

    [
      {
        "_id": {"$oid": "60a7c3d23541f3714eb91332"},
        "email": "rambo@hotmail.com",
        "genres": [18, 28, 53]
      },
      {
        "_id": {"$oid": "60a7c4373541f3714eb9135f"},
        "email": "dexter@hotmail.com",
        "genres": [13, 18, 28, 53]
      }
    ]
    

    数据集:

    [
        {
            "_id" : ObjectId("60a7c3d23541f3714eb91332"),
            "email" : "rambo@hotmail.com",
            "password" : "12345",
            "movies_watched" : [ 
                {
                    "title" : "Rambo",
                    "movie_id" : 7555,
                    "movie_runtime" : 99,
                    "movie_genres" : [ 
                        28, 
                        53
                    ]
                }, 
                {
                    "title" : "Rambo: Last Blood",
                    "movie_id" : 522938,
                    "movie_runtime" : 99,
                    "movie_genres" : [ 
                        28, 
                        53, 
                        18
                    ]
                }
            ]
        },
        {
            "_id" : ObjectId("60a7c4373541f3714eb9135f"),
            "email" : "dexter@hotmail.com",
            "password" : "123456",
            "movies_watched" : [ 
                {
                    "title" : "Dexter",
                    "movie_id" : 444,
                    "movie_runtime" : 99,
                    "movie_genres" : [ 
                        13, 
                        18
                    ]
                }, 
                {
                    "title" : "Rambo: Last Blood",
                    "movie_id" : 522938,
                    "movie_runtime" : 99,
                    "movie_genres" : [ 
                        28, 
                        53, 
                        18
                    ]
                }
            ]
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 2014-10-16
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      相关资源
      最近更新 更多