简体   繁体   中英

How to update a field of an object in array of array in MongoDB?

My mongodb data is like this,a little complicated because of array in array.

{
"_id" : ObjectId("5e36950f65fae21293937594"),
"userId" : "5e33ee0b4a3895a6d246f3ee",
"notes" : [ 
    {
        "noteId" : ObjectId("5e36953665fae212939375a0"),
        "time" : ISODate("2020-02-02T17:24:06.460Z"),
        "memoryLine" : [ 
            {
                "_id" : ObjectId("5e36953665fae212939375ab"),
                "memoryTime" : ISODate("2020-02-03T17:54:06.460Z"),
                "hasReviewed": false
            }, 
            {
                "_id" : ObjectId("5e36953665fae212939375aa"),
                "memoryTime" : ISODate("2020-02-03T05:24:06.460Z"),
                "hasReviewed": false
            }
        ]
    }
]}

I want to update hasReviewed in memoryLine with _id.For example, i want to update _id:ObjectId(5e36953665fae212939375ab) item's hasReviewed to true . As expected like below :

{
"_id" : ObjectId("5e36950f65fae21293937594"),
"userId" : "5e33ee0b4a3895a6d246f3ee",
"notes" : [ 
    {
        "noteId" : ObjectId("5e36953665fae212939375a0"),
        "time" : ISODate("2020-02-02T17:24:06.460Z"),
        "memoryLine" : [ 
            {
                "_id" : ObjectId("5e36953665fae212939375ab"),
                "memoryTime" : ISODate("2020-02-03T17:54:06.460Z"),
                "hasReviewed": true    <============here updated to true
            }, 
            {
                "_id" : ObjectId("5e36953665fae212939375aa"),
                "memoryTime" : ISODate("2020-02-03T05:24:06.460Z"),
                "hasReviewed": false
            }
        ]
    }
]}

How to deal with it?Thank you.

You can use arrayFilters in .updateOne() to do this :

db.collection.update({ 'notes.memoryLine._id': ObjectId("5e36953665fae212939375ab") },
    { $set: { "notes.$[].memoryLine.$[element].hasReviewed": true } },
    { arrayFilters: [{ "element._id": ObjectId("5e36953665fae212939375ab") }] })

Note : Since memoryLine is an array inside an object of notes array then this notes.$[] would update all objects memoryLine.hasReviewed to true when criteria is matched, in other case if you wanted to update just first matched object you can use notes.$ . Also instead of .updateOne() you can use .updateMany() to update multiple documents.

I find a solution for this.

db.notes.updateOne({
    userId:"5e33ee0b4a3895a6d246f3ee",
    'notes.0.memoryLine._id': ObjectId("5e36953665fae212939375ab"),
}, {
    '$set': {
        'notes.0.memoryLine.$.hasReviewed': true
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM