smith hari
smith hari

Reputation: 437

How to update data in mongodb

I tried to array inside to update using mongodb but its throwing error how to solve it.

[
    {
        "_id": "5b4efd6fd53be829188070c8",
        "id": 1,
        "name": "All Categories",
        "hasSubCategory": "false",
        "parentId": "0"
    }
]

I tried this way of code

db_connection.collection('ecomm_prod_db_product').update({_id:product_data[i]['_id']},{$set :{product[i]['name']:"hari}})

but its throwing error

Upvotes: 0

Views: 32

Answers (1)

Joschua Schneider
Joschua Schneider

Reputation: 4093

You are trying to use the value of your product as the object key here.

That will put whatever value lives in your product[i].name as the key like this:

{$set :{ "Old Name For Example": "hari" }

This would try to set a property called Old Name For Example on the document, instead of name **

Instead, you should provide the property name as key, "name"in this case:

{$set :{ name: "hari" }

** (not your usecase but might be noteworthy here)
Should be noted, that this will more likely throw due to the syntax. As the actual usage for a value as a key would be using [myKeyValue] (computed properties).

Upvotes: 1

Related Questions