Home > Enterprise >  How to set "seen:true" in array of messages in mongoose
How to set "seen:true" in array of messages in mongoose

Time:05-10

I am newbie in mongodb world, i was stuck in on Mongoose query.

basically I was developing a chat application for my website. the chat schema of my website shown below

const LiveChat = mongoose.Schema({
    
    members: [String],
    messages: [{
        sender: String,
        reciever: String,
        text: String,
        seen: {
            type: Boolean,
            default: false
        },
        date: {
            type: Date,
            default: Date.now
        }
    }],
}, { timestamps: true });

for exapmle collection will looks like this

[   
    {
     
      "_id":"627749f8dc5927d660f76172",
      "members":[
         "626a250d11ed1b096883aed1",
         "626a234611ed1b096883ae13"
      ],
      "messages":[
         {
            "sender":"626a250d11ed1b096883aed1",
            "reciever":"626a234611ed1b096883ae13",
            "text":"hello there!.",
            "seen":false,
            "_id":"6278b0c38031894a9ceefeae",
            "date":"2022-05-09T06:12:19.857Z"
         },
         {
            "sender":"626a250d11ed1b096883aed1",
            "reciever":"626a234611ed1b096883ae13",
            "text":"how are you?.",
            "seen":false,
            "_id":"6278b0e18031894a9ceefede",
            "date":"2022-05-09T06:12:49.680Z"
         },
         {
            "sender":"626a234611ed1b096883ae13",
            "reciever":"626a250d11ed1b096883aed1",
            "text":"hello Rupesh",
            "seen":false,
            "_id":"6278b1438031894a9ceeff98",
            "date":"2022-05-09T06:14:27.388Z"
         },
         {
            "sender":"626a234611ed1b096883ae13",
            "reciever":"626a250d11ed1b096883aed1",
            "text":"we are doing well",
            "seen":false,
            "_id":"6278b1588031894a9ceeffe0",
            "date":"2022-05-09T06:14:48.203Z"
         },
         {
            "sender":"626a250d11ed1b096883aed1",
            "reciever":"626a234611ed1b096883ae13",
            "text":"okay",
            "seen":false,
            "_id":"6278b1ed8031894a9cef0099",
            "date":"2022-05-09T06:17:17.421Z"
         }
      ],
      "createdAt":"2022-05-08T04:41:28.416Z",
      "updatedAt":"2022-05-09T06:17:17.420Z",
      "__v":0
   },
   {
      
      "_id":"62762021be68a5e2de8dc2d2",
       members: ["626a250d11ed1b096883aed1", "6273bc879ff276ac89f9c4f8"]
      "messages":[
         {
            "sender":"626a250d11ed1b096883aed1",
            "reciever":"6273bc879ff276ac89f9c4f8",
            "text":"hello there!",
            "seen":false,
            "_id":"6277ac0ba5fe501f98e1421e",
            "date":"2022-05-08T11:39:55.263Z"
         },
         {
            "sender":"626a250d11ed1b096883aed1",
            "reciever":"6273bc879ff276ac89f9c4f8",
            "text":"can you please tell me what is the date of start a project task",
            "seen":false,
            "_id":"6277ac30a5fe501f98e1424c",
            "date":"2022-05-08T11:40:32.472Z"
         },
         
    
      ],
      "createdAt":"2022-05-07T07:30:41.447Z",
      "updatedAt":"2022-05-08T12:24:31.400Z",
      "__v":0
   }
   
]

now i want to set seen:true for all messages into messages array whose sender == "626a250d11ed1b096883aed1" ( sender_id and conversation_id are given from req.body into a API). for all messages in messages array: seen:false means message is not seen by reciever seen:true means message is seen by reciever

i was trying following way into my express API but its not wokring...

cosnt {conversation_id, sender_id} = req.body;
LiveChat.findByIdAndUpdate({ _id: conversation_id },
            [{
                $set: {
                    'messages.seen': { $cond: [{ $eq: ['messages.sender', sender_id] }, true, false] }
                }
            }]
            ,
            {
                new: true,
                useFindAndModify: true,
            }

please help me to write this query...

CodePudding user response:

Here's one way you could do the update using "arrayFilters".

db.collection.update({
  "_id": "627749f8dc5927d660f76172"  // given _id
},
{
  "$set": {
    "messages.$[x].seen": true
  }
},
{
  "arrayFilters": [
    {
      "x.sender": "626a250d11ed1b096883aed1"  // given sender
    }
  ]
})

Try it on mongoplayground.net.

  • Related