如何在 mongoose 的消息数组中设置 "seen:true"

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

我是 mongodb 世界的新手,我被困在 Mongoose 查询中。

基本上我是在为我的网站开发一个聊天应用程序。我网站的聊天模式如下所示

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

例如集合将如下所示

[   
    {
     
      "_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
   }
   
]

现在我想将所有消息的 seen:true 设置为 messages 数组,其 sender == "626a250d11ed1b096883aed1"(sender_id 和 conversation_id 由 req.body 变成 API)。 对于 messages 数组中的所有消息: seen:false 表示消息未被接收者看到 seen:true 表示消息被接收者看到

我正在尝试进入我的快递 API 但它不起作用...

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

请帮我写这个查询...

这是一种使用 "arrayFilters" 进行更新的方法。

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

mongoplayground.net 上试用。