0

I'm trying to count the number of different string occurrences in a nested (sub) document in my MongoDB collection called Events.

First:

  • It's looks for the specific Event Id with $match
  • Then it $unwinds the nested document called requestList
  • Then it $unwinds the field in the requestList called songStatus
  • Then it $group and counts ($sum) each different occurrence

With my document it should return An array called data with 3 objects in it: data: Array(3)

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

Instead it returns data: Array(3):

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

Why are they all called queue when there are clearly 3 different values for songStatus in the document? The counts are correct the _ids are not.

Here is my code:

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));
  },

Here is my document:

{
    "_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",
            "generalRequest" : false,
            "playNow" : true,
            "tip" : 25,
            "songStatus" : "queue"
        }, 
        {
            "_id" : ObjectId("604fc1e04c10105a54ae2a7a"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/e06f235d701ef090e8a76e81780c1965.png",
            "customerName" : "Charles Robinson",
            "title" : "Bat Out of Hell",
            "artist" : "Meatloaf",
            "generalRequest" : false,
            "playNow" : true,
            "tip" : 20,
            "songStatus" : "playNowQueue"
        }, 
        {
            "_id" : ObjectId("60510eae71340e09e8a15b69"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c56b06a92b944251b80fc8f4ee32c3fb.png",
            "customerName" : "Charles Robinson",
            "title" : "Everyday",
            "artist" : "Dave Matthews Band",
            "generalRequest" : false,
            "playNow" : true,
            "tip" : 25,
            "songStatus" : "playNowQueue"
        }, 
        {
            "_id" : ObjectId("6051727ffa40160a80fac1e7"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/01bccad9d7be4980c9fdbcec3be695c9.png",
            "customerName" : "Larisse Robinson",
            "title" : "Best Day of My Life",
            "artist" : "American Authors",
            "generalRequest" : true,
            "playNow" : false,
            "tip" : 25,
            "songStatus" : "generalRequestQueue"
        }, 
        {
            "_id" : ObjectId("605172c4fa40160a80fac1e8"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/b0c33f40f31b63909c93f587824347b2.png",
            "customerName" : "Charles Robinson",
            "title" : "Brick House",
            "artist" : "Commodores",
            "generalRequest" : true,
            "playNow" : false,
            "tip" : 20,
            "songStatus" : "generalRequestQueue"
        }, 
        {
            "_id" : ObjectId("605173ced9514e635c806bf9"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/92f33281764af1c194e83a6fbfa3552c.png",
            "customerName" : "Charles Robinson",
            "title" : "Friends In Low Places",
            "artist" : "Garth Brooks",
            "generalRequest" : true,
            "playNow" : false,
            "tip" : 15,
            "songStatus" : "generalRequestQueue"
        }, 
        {
            "_id" : ObjectId("60517401d9514e635c806bfa"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c4b859f726d6ff189fe3ec68a4156dd4.png",
            "customerName" : "Charles Robinson",
            "title" : "Low",
            "artist" : "Flo Rida",
            "generalRequest" : true,
            "playNow" : false,
            "tip" : 15,
            "songStatus" : "generalRequestQueue"
        }, 
        {
            "_id" : ObjectId("6052740ad19a1b186035569a"),
            "albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/f46fad9ede1dc6bd1e653f0fe79e6fc2.png",
            "customerName" : "Charles Robinson",
            "title" : "Sexy And I Know It",
            "artist" : "LMFAO",
            "generalRequest" : true,
            "playNow" : false,
            "tip" : 25,
            "songStatus" : "generalRequestQueue"
        }
    ],
    "__v" : 0
}

1 Answers1

0

Problem solved! Mongodb not-apparently didn't like my hodge podge requestList because I updated the model and didn't drop the DB. I just manually added the fields. I dropped the DB and it work as designed! Lesson learned there!