0

What is the best way to update the array element inside the array in MongoDB? For example, the data looks like this:

{
        "_id" : ObjectId("6201396b866ffbf1b84fb8f9"),
        "title" : "ironman",
        "comments" : [
                {
                        "text" : "nihao",
                        "replies" : [
                                {
                                        "text" : "hi"
                                },
                                {
                                        "text" : "bonjour"
                                },
                                {
                                        "text" : "push replies!!!"
                                }
                        ]
                },
                {
                        "text" : "what??",
                        "replies" : [
                                {
                                        "text" : "the"
                                },
                                {
                                        "text" : "hey"
                                }
                        ]
                },
                {
                        "text" : "push comments!!!"
                }
        ]
}

I want to change

"comments.replies.text: 'hi'"

to

"comments.replies.text: 'hello'"

What would be the best way to write a query if you want to update the elements inside replies?

4
  • There are many array update operators - you use then depending upon your use case. There are multiple replies within the comments array field. You need to tell which array element you want to update, etc. (more details). See Array Update Operators. Commented Feb 8, 2022 at 10:29
  • I can update comments by using db.movies.updateOne( { title: 'ironman', 'comments.text': 'hello' }, { $set: { 'comments.$.text': 'nihao' } } ) but, replies coudln't update the manner, ??? db.movies.updateOne( { title: 'ironman', 'comments.text': 'nihao' }, { { $set: { 'comments.$.replies.$.text': 'nihao' } } } ) Commented Feb 8, 2022 at 10:40
  • You want to update all replies - from hi to hello? You can use the arrayFilters option. Commented Feb 8, 2022 at 10:50
  • oh no, only replies - hi to hello. one element. "replies" : [ { "text" : "hi" }, { "text" : "bonjour" }, -> "replies" : [ { "text" : "hello" }, { "text" : "bonjour" }, Commented Feb 8, 2022 at 11:04

1 Answer 1

1

You need $[<identifier>] filtered positional operator and arrayFilters to update nested document(s) in the array.

db.collection.update({
  title: "ironman"
},
{
  $set: {
    "comments.$[comment].replies.$[reply].text": "hello"
  }
},
{
  arrayFilters: [
    {
      "comment.replies": {
        $exists: true
      }
    },
    {
      "reply.text": "hi"
    }
  ]
})

Sample Demo on Mongo Playground

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, solved this problem!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.