F
F
fostsinger2020-08-11 05:48:00
Node.js
fostsinger, 2020-08-11 05:48:00

Mongodb aggregate data for one user?

you need to count all unread messages for one user for each chat separately

{
 "_id" : ObjectId("5f315c1d80d6ce3f7bb597ba"),
    "client" : ObjectId("5f2071c14bdf84841fb1b6a0"),
    "read" : false,
    "ticket" : ObjectId("5f315bec80d6ce3f7bb597b9"),
    "message" : "00000",
    "createdAt" : ISODate("2020-08-10T14:39:25.893Z"),
    "__v" : 0
},
{
 "_id" : ObjectId("5f315c1d80d6ce3f7bb597ba"),
    "client" : ObjectId("5f2071c14bdf84841fb1b6a0"),
    "read" : false,
    "ticket" : ObjectId("5f315bec80d6ce3f7bb597b9"),
    "message" : "00000",
    "createdAt" : ISODate("2020-08-10T14:39:25.893Z"),
    "__v" : 0
}

below query gives all tickets of all users and their counts
db.chatContacts.aggregate([
    {$match: {read: false, client: {$ne: auth.user._id}}}, {
      $group: {
        _id: '$ticket',
        count: {$sum: 1}
      }
    }])

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question