I had a json which is inserted in my mongo collection.Now I want to update the same json that is inserted in the collection,in such a way that I need to add new data element to that collection using the id which will be generated earlier. eg:
{
 "name":"sam",
 "age":20,
  "_id":""5sq8uye1236g'
 }
If above is the json what is inserted in one collection where we have _id generated for it in collection.
Now I had variable called token .
var token=12345
Now I want to update the above collection such that this token gets added to it based on the _id of that.
Required format in collection:
{
"name":"sam",
"age":20,
 "token":12345,
"_id":""5sq8uye1236g'
 } 
How can I do that?
Use update command.
Your query should look like
db.collection.update({_id:'5sq8uye1236g'},{$set :{token :12345});
Read more about updating here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With