[英]Update multiple fields in mongoose subdocuments using $push
I have a conversation schema which will contain two users.我有一个包含两个用户的对话模式。 I want to flag messages to be deleted from one user so the other recipient still able to read the message.我想标记要从一个用户处删除的邮件,以便其他收件人仍然能够阅读该邮件。
Schema架构
// Messages Schema
var messagesSchema = new Schema({
from: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User',
},
content: {
type: String,
required: true,
trim: true
},
deleted_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
read_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
}, {
timestamps: true
});
// Conversation Schema
var conversationsSchema = new Schema({
recipients: [{
type: Schema.Types.ObjectId,
required: true,
ref: 'User',
index: true
}],
messages: [messagesSchema],
}, {
timestamps: true
});
Now when I create a conversation between two users it will look like the following现在,当我在两个用户之间创建对话时,它将如下所示
[
{
"_id": "57bb6fed3d001f054e809175",
"updatedAt": "2016-08-22T21:37:30.631Z",
"createdAt": "2016-08-22T21:34:37.381Z",
"__v": 2,
"messages": [
{
"updatedAt": "2016-08-22T21:34:37.380Z",
"createdAt": "2016-08-22T21:34:37.380Z",
"from": "57b7448668d04d3035774b9a",
"content": "Hello are you there?",
"_id": "57bb6fed3d001f054e809176",
"read_by": [],
"deleted_by": []
},
{
"updatedAt": "2016-08-22T21:34:58.060Z",
"createdAt": "2016-08-22T21:34:58.060Z",
"from": "57b7448668d04d3035774b9a",
"content": "I miss you",
"_id": "57bb70023d001f054e809177",
"read_by": [],
"deleted_by": []
},
{
"updatedAt": "2016-08-22T21:37:30.631Z",
"createdAt": "2016-08-22T21:37:30.631Z",
"from": "57b7816b68d04d3035774b9b",
"content": "Hey... Me too",
"_id": "57bb709a3d001f054e809178",
"read_by": [],
"deleted_by": []
}
],
"recipients": [
"57b7448668d04d3035774b9a",
"57b7816b68d04d3035774b9b"
]
}
]
Now when one of the users want to delete the conversation from his side I want to add the user id to the deleted_by
array inside each message.现在,当其中一个用户想要从他身边删除对话时,我想将用户 ID 添加到每条消息中的deleted_by
数组中。
I am trying to do something like this我正在尝试做这样的事情
Conversation.findOneAndUpdate({
_id: conversation_id
}, {
$push: {
'messages.deleted_by': req.loggedInUser._id
}
}, function(err, data) {
if(err) return next(err);
res.json(data);
})
Error Returned错误返回
TypeError: Cannot read property '$isMongooseDocumentArray' of undefined
I tried to add the $
sign and still getting the same error.我尝试添加$
符号,但仍然出现相同的错误。
Try this:试试这个:
Conversation.findOne({
_id: conversation_id
}, function(err, docs) {
if(err) return next(err);
if(docs)
{
docs.messages.forEach(function(msg,index,array)
{
msg.deleted_by.push(req.loggedInUser._id);
});
docs.save();
}
});
Read this for better understanding of forEach function.阅读本文以更好地理解 forEach 函数。
I hope this helps.我希望这会有所帮助。
You can push multiple items just by adding the parameters in the $push field只需在 $push 字段中添加参数即可推送多个项目
Example:$push:{<field1>:<value1>,<field2>:<value2>}
示例:$push:{<field1>:<value1>,<field2>:<value2>}
Conversation.findOneAndUpdate({
_id: conversation_id},
{$push: {messages.deleted_by: req.loggedInUser._id,read_by: //your_fields }
}, function(err, data) {
if(err) return next(err);
res.json(data);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.