Home > front end >  How to update my nested array of another nested array in object in mongodb
How to update my nested array of another nested array in object in mongodb

Time:03-28

My problem is somehow related to this problem LINK. However this is my problem..

{
    "_id": ObjectId("4d2d8deff4e6c1d71fc29a07"),
    "comments": [{
            "name": "Len",
            "_id": ObjectId("6241108375ed952ceb0187e3"),
            "data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             }]
        } {
            "name": "Lilipad",
            "_id": ObjectId("6241108375ed952ceb0187e5"),
            "data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             }]
        } {
            "name": "Leni Robredo",
            "_id": ObjectId("6241108375ed952ceb0187e9"),
            "data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             }]
        }
        ...
    ]
}

Now, what I want to happen here is when you get the _id of the comments such as ObjectId("6241108375ed952ceb0187e3"),ObjectId("6241108375ed952ceb0187e5"),ObjectId("6241108375ed952ceb0187e9"). I want update a new set of data for each of them, but I am so confused how I am gonna do it with the operator. This is how my inputs gonna work and the output.

parent_id = ObjectId("4d2d8deff4e6c1d71fc29a07")
sub_id = ObjectId("6241108375ed952ceb0187e5")
new_data = [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             },{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb012131")
             }]

Output

{
    "_id": ObjectId("4d2d8deff4e6c1d71fc29a07"),
    "comments": [{
            "name": "Len",
            "_id": ObjectId("6241108375ed952ceb0187e3"),
            "data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             }]
        } {
            "name": "Lilipad",
            "_id": ObjectId("6241108375ed952ceb0187e5"),
            "new_data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             },{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb012131")
             }]
        } {
            "name": "Leni Robredo",
            "_id": ObjectId("6241108375ed952ceb0187e9"),
            "data": [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             }]
        }
        ...
    ]
}

As you see I use sub_id to get the id of the item of an array that I want to update its data..because I can use the same name but not the id so that's why I used _id of array items of comments.

Now what If I want to my pass new all data in all id of comment array.. I already prepare a loop for that let say something likes this.

list_comment_id = [ObjectId("6241108375ed952ceb0187e3"),
ObjectId("6241108375ed952ceb0187e5"),ObjectId("6241108375ed952ceb0187e9")]

parent_id = ObjectId("4d2d8deff4e6c1d71fc29a07")
new_data = [{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187dd")
             },{
                "name":"Lilipad",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb0187da")
             },{
                "name":"Len",
                "confirmation_likes":"false",
                "_id":ObjectId("6241102d75ed952ceb012131")
             }]

And so that i will pass up all the data that my list_comment_id wants..

I already tried something like this in my code

router.route('/append/:id').put((req,res) => {
    const { _id, sub_id, data} = req.body
    const main_id = req.params.id

    // Sub_id for comments _id items
    COMMENT.updateOne(
        {
            _id:main_id,
            "comments._id" : sub_id
        },
        {
            $set:{
                data:data
            }
        }
    ).then((comments) => res.json("Likes List People Updated") )
    .catch(err => console.log(err))

})

Anyone can help me please thank you?? I'll be gladly thankful.

CodePudding user response:

Maybe something like this:

db.collection.update({
 _id: ObjectId("4d2d8deff4e6c1d71fc29a07")
},
{
$set: {
"comments.$[x]": {
  "name": "Lilipad",
  "_id": ObjectId("6241108375ed952ceb0187e5"),
  "new_data": [
    {
      "name": "Len",
      "confirmation_likes": "false",
      "_id": ObjectId("6241102d75ed952ceb0187dd")
    },
    {
      "name": "Lilipad",
      "confirmation_likes": "false",
      "_id": ObjectId("6241102d75ed952ceb0187da")
    },
    {
      "name": "Len",
      "confirmation_likes": "false",
      "_id": ObjectId("6241102d75ed952ceb012131")
    }
   ]
  }
 }
},
{
 arrayFilters: [
 {
    "x._id": ObjectId("6241108375ed952ceb0187e5")
 }
]
})

Explained: Create arrayFilters x to identify which element need to be updated and update with the necessary values.

playground

  • Related