使用$ push更新mongoose子文档中的多个字段

时间:2016-08-23 00:01:35

标签: node.js mongoose

我有一个会话架构,其中包含两个用户。我想标记要从一个用户删除的邮件,以便其他收件人仍然能够阅读该邮件。

模式

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

现在,当我在两个用户之间创建对话时,它将如下所示

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

现在,当其中一个用户想要从他身边删除对话时,我想将用户ID添加到每条消息中的deleted_by数组中。

我正在尝试做这样的事情

Conversation.findOneAndUpdate({
    _id: conversation_id
}, {
    $push: {
        'messages.deleted_by': req.loggedInUser._id
    }
}, function(err, data) {
    if(err) return next(err);
    res.json(data);
})

错误返回

TypeError: Cannot read property '$isMongooseDocumentArray' of undefined

我尝试添加$符号并仍然收到相同的错误。

2 个答案:

答案 0 :(得分:0)

试试这个:

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

阅读this以更好地了解forEach功能。

我希望这会有所帮助。

答案 1 :(得分:0)

<块引用>

只需在 $push 字段中添加参数即可推送多个项目
示例:$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);
 })