Nishchit
Nishchit

Reputation: 19094

Mongo query to update field

i am using Mongo library(alex bibly) with codeignitor , and my collection like this

{
  chart:{
         "LL":[
                { "isEnable":true,
                  "userName":"Nishchit Dhanani"
                }
              ]           
        }
}

I want to update isEnable=false .

Any help appreciated .

Upvotes: 0

Views: 126

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222771

First of all you have an error in your JSON document. You can not have key values in a dictionary.

So your JSON should be like this:

{
    "_id" : ObjectId("526e0d7ef6eca1c46462dfb7"),  // I added this ID for querying. You do not need it
    "chart" : {
        "LL" : {
            "isEnable" : false,
            "userName" : "Nishchit Dhanani"
        }
    }
}

And to do what you needed you have to use $set

db.test.update(
  {"_id" : ObjectId("526e0d7ef6eca1c46462dfb7")},
  {$set : {"chart.LL.isEnable" : false}
})

With your new modification, you need to do something like this:

db.test.update(
  {"chart.LL.isEnable" : false},
  {$set : {"chart.LL.$.isEnable" : false}}
)

Upvotes: 1

Related Questions