Mongodb / mongoose 聚合 - 计算嵌套文档中某个字段的字符串出现次数,它返回具有相同名称的 _id - 计数正确

问题描述

我正在尝试计算我的 MongoDB 集合中名为 Events 的嵌套(子)文档中不同字符串出现的次数

首先:

  • 它使用 $match 查找特定的事件 ID
  • 然后它$展开名为 requestList 的嵌套文档
  • 然后 $unwinds requestList 中名为 songStatus 的字段
  • 然后它 $group 并计算 ($sum) 每个不同的出现

我的文档应该返回 一个名为 data 的数组,其中包含 3 个对象: 数据:数组(3)

  • 0: {_id: "generalRequestQueue",count: 5}
  • 1: {_id: "playNowQueue",count: 3}
  • 2: {_id: "queue",count: 1}

相反,它返回 数据:数组(3):

  • 0: {_id: "queue",count: 5}
  • 1: {_id: "queue",count: 1}

当文档中的songStatus明显有3个不同的值时,为什么它们都被称为队列? 计数正确,_id 不正确。

这是我的代码

countSongStatuses: function (req,res) {
    db.Event.aggregate([
      // Limit matching documents (can take advantage of index)
      {
        $match: {
          "_id": req.params.id
        }
      },// Unpack the requestList and songStatuses
      { "$unwind": "$requestList" },{ "$unwind": "$requestList.songStatus" },// Group by the answer values
      {
        "$group": {
          "_id": "$requestList.songStatus",count: { $sum: 1 }
        }
      }])
      .then((dbModel) => res.json(dbModel))
      .catch((err) => res.status(422).json(err));
  },

这是我的文档:

{
    "_id" : "55f536a-2186-56d-4e23-54dd657178","subIdForEventStatusChange" : "55f536","eventStatus" : "activated","genre" : "Everything!","eventDate" : ISODate("2021-05-05T00:00:00.000Z"),"startTime" : "5:59pm","endTime" : "9:00pm","eventName" : "Cinco de Mayo Celebration","eventType" : "Dinner Dance Party","venueName" : "Casa de Pico","venueAddress" : "1234 La Mesa Blvd.; La Mesa,CA 91942","eventimage" : "https://res.cloudinary.com/noimgmt/image/upload/v1615839494/noireqapp/yd5bfcxh7cw5e2fg2wof.jpg","requestList" : [ 
        {
            "_id" : ObjectId("604fc1504c10105a54ae2a78"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/fc2eb7a560c2a46acbb17362bb373c59.png","customerName" : "Larisse Robinson","title" : "Firework","artist" : "Katy Perry","generalRequest" : false,"playNow" : true,"tip" : 100,"songStatus" : "playNowQueue"
        },{
            "_id" : ObjectId("604fc1ad4c10105a54ae2a79"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/a1ef00507ce94ac6b59d00d6603a1a22.png","customerName" : "Charles Robinson","title" : "Around the World","artist" : "Red Hot Chili Peppers","tip" : 25,"songStatus" : "queue"
        },{
            "_id" : ObjectId("604fc1e04c10105a54ae2a7a"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/e06f235d701ef090e8a76e81780c1965.png","title" : "Bat Out of Hell","artist" : "Meatloaf","tip" : 20,{
            "_id" : ObjectId("60510eae71340e09e8a15b69"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c56b06a92b944251b80fc8f4ee32c3fb.png","title" : "Everyday","artist" : "Dave Matthews Band",{
            "_id" : ObjectId("6051727ffa40160a80fac1e7"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/01bccad9d7be4980c9fdbcec3be695c9.png","title" : "Best Day of My Life","artist" : "American Authors","generalRequest" : true,"playNow" : false,"songStatus" : "generalRequestQueue"
        },{
            "_id" : ObjectId("605172c4fa40160a80fac1e8"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/b0c33f40f31b63909c93f587824347b2.png","title" : "Brick House","artist" : "Commodores",{
            "_id" : ObjectId("605173ced9514e635c806bf9"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/92f33281764af1c194e83a6fbfa3552c.png","title" : "Friends In Low Places","artist" : "Garth brooks","tip" : 15,{
            "_id" : ObjectId("60517401d9514e635c806bfa"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c4b859f726d6ff189fe3ec68a4156dd4.png","title" : "Low","artist" : "Flo Rida",{
            "_id" : ObjectId("6052740ad19a1b186035569a"),"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/f46fad9ede1dc6bd1e653f0fe79e6fc2.png","title" : "Sexy And I KNow It","artist" : "LMFAO","songStatus" : "generalRequestQueue"
        }
    ],"__v" : 0
}

解决方法

问题解决了! Mongodb 显然不喜欢我的大杂烩 requestList,因为我更新了模型并且没有删除数据库。我只是手动添加了字段。我放弃了数据库,它按设计工作!在那里学到的教训!