How to update the nested array element which is a nested document

{
        "_id" : ObjectId("60409b54bb80580eb14aebb7"),
        "firstName" : "Luna",
        "lastName" : "fix",
        "phoneNumber" : "323-123-3232",
        "email" : "",
        "address" : [
                {
                        "street" : "XXXXX"
                }
        ],
        "createdAt" : ISODate("2021-03-04T08:33:24.921Z"),
        "updatedAt" : ISODate("2021-03-04T08:33:24.921Z"),
        "__v" : 0
}

how can i update the ‘street’ value from ‘XXXXX’ to another value?
I’ve tried this and tried looking up online but doesnt help, Please help me.

db.collection.update({_id: ObjectId("60409b54bb80580eb14aebb7")}, 
                        {$set: {"address.street": "New Land"}})

Hi @M_San_B,

Welcome to MongoDB community.

What’s the problem with the provided command it looks correct.

Thanks
Pavel

Thanks for providing a well formatted complete sample document. It saves us some time when trying to replicate the issue.

Try with the following:

db.collection.update({_id: ObjectId("60409b54bb80580eb14aebb7")}, 
                        {$set: {"address.0.street": "New Land"}})

The only difference is the added array element index.

1 Like

@steevej that works, thank you.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.