我有一個聊天應用程式,我正在嘗試seen在其上添加功能。我正在嘗試執行一個可以更新所有聊天訊息(已看到)列的查詢。
這是架構:
const messageSchema = new mongoose.Schema({
senderId: {
type: String,
required: true
},
message: {
type: String,
required: true
},
seen: { // I'm trying to update this in all the documents !!
type: Boolean,
default: false
}
}, {timestamps: true});
const chatSchema = new mongoose.Schema({
messages: [messageSchema]
});
正如您在此處看到的,我seen在訊息模式中有一個屬性(嵌套在聊天模式中)
所以我只想進行一次聊天,更新其中的所有訊息并將seen列更新為true
我試過了:
const messagesSeen = async (req, res) => {
const chatId = req.params.id;
await Chat.findByIdAndUpdate(
chatId,
{
$set: { 'messages.seen': true },
},
{ new: true }
)
.then((chat) => {
return res
.status(200)
.json({ chat });
})
.catch((error) => {
return res.status(500).json({ message: error.message });
});
};
但不幸的是,它沒有用
所以,希望能找到解決辦法。謝謝
uj5u.com熱心網友回復:
您應該使用位置運算子- $[]。
await Chat.findByIdAndUpdate(
chatId,
{
$set: { "messages.$[].seen": true },
},
{
new: true
})
作業示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431859.html
標籤:javascript 节点.js mongodb 表示 猫鼬
