首页 > 解决方案 > 更新 MongoDB 中的子子数组项

问题描述

我正在尝试更新 MongoDB 中的子子数组项。我想更新幻灯片数组中的评论(评论字段)。

幻灯片--> 评论--> 评论

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var AttachmentSchema = new Schema({
   FileName: String,
   InternalName: String,
   FileType: String,
   InternalName: String
 });

mongoose.model('Attachment', AttachmentSchema);

var SessionSchema = new Schema({
SessionTitle: String,
Chairperson: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
StartDateTime: { type: Date, required: true },
Duration: Number,
Attendees: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
AcceptOrReject: {
    Comment: String,
    Reason: String,
    CreatedDate: Date,
    UserId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    Status: String
},
Slides: [{
    Seq: Number,
    Details: String,
    attachement: AttachmentSchema,
    TarasulLetterDetails:{
        MailObjectId: String,
        Description: String,
        Subject: String,
        TarasulLetterUrl: String
    },
    Comments: [{
        Comment: String,
        DateTime: Date,
        UserId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        IsFromOnline : String
    }],
    SurfaceAttachement: [{
        FileName: String,
        InternalName: String,
        FileType: String,
    }]
}],
Status: String,
CreatedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
CreatedDate: { type: Date, required: true },
ModifiedDate: Date,
SentDate: Date,
Comments: [{
    Comment: String,
    DateTime: Date,
    UserId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}],
Notifications: [{
    UserId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    CreatedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    AlertType: String,
    AlertMessage: String,
    DateTime: { type: Date, required: true },
    IsAcknowledged: Boolean
}]
});
mongoose.model('Session', SessionSchema);

下面是样本数据。

> db.sessions.find({"_id": ObjectId('5af7bcdff107cb7a105243f5')}).pretty();
  {
    "_id" : ObjectId("5af7bcdff107cb7a105243f5"),
    "SessionTitle" : "Project Status Meeting",
    "Duration" : 60,
    "StartDateTime" : ISODate("2018-05-15T05:15:00Z"),
    "CreatedBy" : ObjectId("5aead4431a7ecc7230fb249a"),
    "Status" : "APPROVED",
    "CreatedDate" : ISODate("2018-05-13T04:19:43.050Z"),
    "Notifications" : [
            {
                    "IsAcknowledged" : false,
                    "DateTime" : ISODate("2018-05-14T09:15:23.385Z"),
                    "AlertMessage" : "Attendee updated",
                    "AlertType" : "Attendee",
                    "UserId" : ObjectId("5aead4a81a7ecc7230fb249c"),
                    "_id" : ObjectId("5af953ab67f3be54d4256f35")
            },
    ],
    "Comments" : [ ],
    "Slides" : [
            {
                    "Seq" : 1,
                    "Details" : "<p>Project Meeting</p>\n",
                    "_id" : ObjectId("5af7bcdff107cb7a105243f6"),
                    "Comments" : [
                            {
                                    "_id" : ObjectId("5b45b854d217f62010fea8e7"),
                                    "DateTime" : ISODate("2018-07-11T07:53:40.967Z"),
 "IsFromOnline" : "3047",
                                    "UserId" : ObjectId("5aead4431a7ecc7230fb249a"),
                                    "Comment" : "test 123"
                            },
                            {
                                    "_id" : ObjectId("5b45b89e7c32803a282e44db"),
                                    "IsFromOnline" : "3043",
                                    "DateTime" : ISODate("2018-07-11T07:58:22.648Z"),
                                    "UserId" : ObjectId("5aead4431a7ecc7230fb249a"),
                                    "Comment" : "new comments"
                            }
                    ]
            },
            {
                    "Details" : "<p>second side</p>\n",
                    "Seq" : 2,
                    "_id" : ObjectId("5af7c843f107cb7a105243f7"),
                    "Comments" : [
                            {
                                    "_id" : ObjectId("5b45b9bc569a4b284074fd08"),
                                    "IsFromOnline" : "92179",
                                    "DateTime" : ISODate("2018-07-11T08:03:01.851Z"),
                                    "UserId" : ObjectId("5aead4431a7ecc7230fb249a"),
                                    "Comment" : "test comments 123434343434343"
                            }
                    ]
            }
    ],
    "Attendees" : [
            ObjectId("5aead4a81a7ecc7230fb249c")
    ],
    "Chairperson" : [
            ObjectId("5aead4701a7ecc7230fb249b")
    ],
    "__v" : 0,
    "AcceptOrReject" : [
            {
                    "Reason" : "More changes neened",
                    "Status" : "APPROVED",
                    "UserId" : ObjectId("5aead4701a7ecc7230fb249b"),
                    "CreatedDate" : ISODate("2018-05-14T09:15:23.385Z"),
                    "Comment" : "Will disucuss in next meeting"
            }
    ]
 }

例如,我想更新会话“_id”:ObjectId("5af7bcdff107cb7a105243f5") 具有 Seq :1 的幻灯片和使用“IsFromOnline”:“3043”从“新评论”到“更新评论”的评论。

我尝试了下面和其他一些东西,但它似乎不起作用。

   Session.update({
   $and: [{
    "_id": req.body.SessionId,"Slides.Seq": req.body.SeqNo
    },
    {
        'Slides.Comments': {
            $elemMatch: {
                IsFromOnline: req.body.IsFromOnline
            }
        }
    }
  ]
  }, {
     $set: {
     'Slides.Comments.$.Comment': 'Updated comments'
   }
})

标签: node.jsmongodb

解决方案


推荐阅读