BhaskerYadav
BhaskerYadav

Reputation: 589

Mongo groupby date inside array

I have collection in my db as,

[
    {
        "groupName" : "testName",
        "participants" : [
            {
                "participantEmail" : "[email protected]",
                "lastClearedDate" : 12223213123
            },
             {
                "participantEmail" : "[email protected]",
                "lastClearedDate" : 1234343243423
            }
        ],
        "messages" : [
            {
                "message":"sdasdasdasdasdasd",
                "time":22312312312,
                "sender":"[email protected]"
            },
            {
                "message":"gfdfvd dssdfdsfs",
                "time":2231231237789,
                "sender":"[email protected]"
            }
        ]
    }
]

This is a collection of group which contains all the participants and messages in that group. The time field inside the message is Timestamp.

I want get all the messages inside a group which are posted after the given date and grouped by date. I wrote the following code,

      ChatGroup.aggregate([
        { $match: { group_name: groupName } },
        { $unwind: "$messages" },
        { $match: { "messages.time": { $gte: messagesFrom } } },
        {
          $project: {
            _id: 0,
            y: {
              $year: {
                $add: [new Date(0), { $multiply: [1000, "$messages.time"] }]
              }
            },
            m: {
              $month: {
                $add: [new Date(0), { $multiply: [1000, "$messages.time"] }]
              }
            },
            d: {
              $dayOfMonth: {
                $add: [new Date(0), { $multiply: [1000, "$messages.time"] }]
              }
            }
          }
        },
        {
          $group: {
            _id: {
              year: "$y",
              month: "$m",
              day: "$d"
            },
            messages: { $push: "$messages" },
            count: { $sum: 1 }
          }
        }
      ]).then(
        group => {
          console.log("length of messages", group);
          resolve(group);
        },
        err => {
          console.log(err);
        }
      );
    });

and I getting the following output,

[
    {
        "_id": {
            "year": 50694,
            "month": 9,
            "day": 5
        },
        "messages": [],
        "count": 3
    },
    {
        "_id": {
            "year": 50694,
            "month": 8,
            "day": 27
        },
        "messages": [],
        "count": 1
    },
    {
        "_id": {
            "year": 50694,
            "month": 8,
            "day": 26
        },
        "messages": [],
        "count": 10
    }
]

I am not getting the messages but the count is correct. Also the time which is displayed in the result is incorrect e.g. year, date and month. Mongo version is 3.2.

I referred the groupby and push documentation from mongodb along with other stackoverflow questions on mongo group by.

What am I doing wrong?

Upvotes: 1

Views: 326

Answers (2)

Ashh
Ashh

Reputation: 46451

Your timestamp is already in seconds. So, you don't need to convert them to millisecond by multiplying with 1000.

So your final query should be something like this

ChatGroup.aggregate([
  { "$match": {
    "group_name": groupName,
    "messages.time": { "$gte": messagesFrom }
  }},
  { "$unwind": "$messages" },
  { "$match": { "messages.time": { "$gte": messagesFrom }}},
  { "$group": {
    "_id": {
      "year": { "$year": { "$add": [new Date(0), "$messages.time"] }},
      "month": { "$month": { "$add": [new Date(0), "$messages.time"] }},
      "day": { "$dayOfMonth": { "$add": [new Date(0), "$messages.time"] }}
    },
    "messages": { "$push": "$messages" },
    "count": { "$sum": 1 }
  }}
])

Upvotes: 1

IftekharDani
IftekharDani

Reputation: 3729

Add messages in $project

{
          $project: {
            _id: 0,
            messages : 1,
           .........
        },
}

Upvotes: 1

Related Questions