Building a web service with Node.js in Visual Studio Part 3: MongoDb basics cont’d


In the previous post we set up MongoDb and looked at the basics of querying against a MongoDb database. We also inserted a couple of customer objects with empty order arrays. Therefore we are familiar with the basics of insertions and querying in MongoDb.

In this post we’ll look at how to perform updates and deletions. Connect to the MongoDb through a command line like we saw in the previous post and get ready for some JavaScript.


Reference: modifying documents.

In case we’d like to update the name of a customer we can do it as follows using the $set operator:

db.customers.update({name: "Mickey Mouse"}, {$set: {name: "Pluto"}})

This will change the customer name “Mickey Mouse” to “Pluto”. If everything went fine then you’ll get a WriteResult statement in the command prompt with fields like nMatched: 1, nUpserted: 0, nModified: 1. You can probably guess that nMatched means the update operation found one matching documents. nModified means the number of documents modified. nUpserted is a mix of “updated” and “inserted”. An upsert is an update operation where a new document is inserted if there are no matching ones.

Read through the reference material above – it’s not long – and note the following:

  • An update operation will by default only update the first matching document – you can override this with the multi flag
  • By default no upsert will be performed in case the search doesn’t result in any document – you can override this with the upsert flag

Updating the orders array of a customer is very similar:

db.customers.update({name: "Great customer"}, {$set: {orders: [{"item": "Book",	"quantity": 2,	"itemPrice": 10  }, {"item": "Car",	"quantity": 1,	"itemPrice": 2000  }]}})

This will update the “orders” property of the customer whose name is “Great customer”. Note that this statement will update the customer’s orders and overwrite any existing orders array, much like the the UPDATE statement in SQL. How can we then insert a new item to an existing orders array? The $push operator comes to the rescue:

db.customers.update({name: "Great customer"}, {$push: {orders: {"item": "Pen",	"quantity": 5,	"itemPrice": 2  }}})


Reference: removing documents.

To remove all customers with the name Pluto execute the following command:

db.customers.remove({name: "Pluto"})

The console output will show in a property called nRemoved how many matching documents were removed.

If you only want to remove the first matching document then indicate it in an index parameter:

db.customers.remove({name: "Mickey Mouse"}, 1)

What if you’d like to remove an item from the orders array? You cannot do that with the remove statement. After all it’s not really a deletion of a customer element but an update of a nested array. The $pull operator will perform what we’re after:

db.customers.update({name: "Great customer"}, {$pull: {orders: {item: "Pen"} } })

This will remove the first element in the orders array of “Great customer” whose item name is “Pen”. If you’d like to remove all array elements with item name “Pen” then use the multi flag:

db.customers.update({name: "Great customer"}, {$pull: {orders: {item: "Pen"} } }, {multi: true})

This should suffice for now. It’s good practice to test some queries based on the MongoDb reference manual. Most of it can be directly used in Node.js as we’ll see later.

In the next post we’ll discuss the basics of a Node.js application.

View all posts related to Node here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: