How to update nested object property in MongoDB

I have a data on MongoDB database which structure is

{
    _id: 12478565,
    products: { name: 'Mackbook Pro', price: 150000, color: 'silver' }
}

I want to update the products price and I write like this, but it gives an error

productsCollection.updateOne({_id: '12478565'}, {$set: {products.price: 190000} })

I want to know how can I modify the data in nested object?

>Solution :

put product.price in qoute

db.collection.update({
  _id: "12478565"
},
{
  $set: {
    "products.price": 190000
  }
})

https://mongoplayground.net/p/QxAK0LClF-8

Leave a Reply