top of page

Updating MongoDB documents - Part 9

Fortunately it is pretty easy to update documents in MongoDB. We use db.products.update(), to update any document. Just now we inserted a product with name “laptop” and price 1200. Now I want to update price to 1000.

db.products.update({name:’laptop’},{$set:{price:1000}})

What if there’s more than one matching record? For example, if I base this on name, I can see I have two from smartphone. Will it change all of them? Well, let’s take a look. db.products.update, and again we’re going to tell it what to update on, let’s say, name:”smartphone” – $set colon; this time I’m going to set the type: I’m going to set it to mobile.

db.products.update({name: “smartphone”},{$set:{type:’mobile’}},{multi: true})

similarly, we can add new field just by using this command. Example as below

db.products.update({name:”smartphone”},{$set:{condition: “New”}})

RECENT POSTS

FEATURED POSTS

Check back soon
Once posts are published, you’ll see them here.

FOLLOW US

  • Grey Facebook Icon
  • Grey Twitter Icon
  • Grey Instagram Icon
  • Grey Google+ Icon
  • Grey Pinterest Icon
bottom of page