This entire week, I am going to discuss the Fundamentals of MongoDB. Today is the fourth post in the six post series. We are going to learn about CRUD Operation: Updating Objects in MongoDB. I have recently build a Video Course at Foundations of Document Databases with MongoDB focusing on helping users to get started with MongoDB quickly and efficiently.
CRUD Operation: Update Operation
CRUD is an acronym and it stands for Create, Read, Update, and Delete. MongoDB is a NoSQL database and that means along with SQL concepts, it is also following NoSQL concepts.
For MongoDB Update Operations actually stands for modifying and replacing documents from the collection.
Here are the most popular read methods in MongoDB.
db.collection.updateOne() – Updates a single document in the collection.
db.collection.updateMany() – Updates a multiple document in the collection.
db.collection.replaceOne() – Replaces a older documents with a new document in the collection.
Updating Objects
MongoDB is schemaless and that makes the update operation pretty robust most of the time and unpredictable many times as well. MongoDB updates are case sensitive as well (based on your collation), so when you update column Title it is not the same as updating the column title.
It is important to remember a few important strategies when updating the _id field as well as atomicity.
Updating _id field
For every single update, there is one rule which is very critical and it is about _id field. Once you have a document with a -id field, you can’t change that _id field. If you try to update the _id field with different values, it will give you an error. essentially, _id field is like a primary key or unique identifier for each document.
Updating Documents
Let us understand some of the keywords which will help us update documents from our collection.
We will be using the updateOne() or updateMany() methods to update our documents. It takes many parameters but we will discuss three of the important parameters. The first parameter is the query and the second parameter is the update containing modifications to apply. The third parameter is actually a boolean datatype parameter upsert.
db.collection.find(query, update, upsert)
When upsert is set to true for updateOne, it creates a new document if no documents match the filter. So essentially it is an update and if not found insert.
Here are a few sample commands to update documents inside the collections named movie.
Update the movie where the title is “Old School” and replace it with “New School”.
db.movies.updateOne( {title: {$eq: "Old School"}}, { $set: { "title": "New School" } } )
Update the movie where the title is “Old School” and replace it with “New School” and set releaseYear to 2010.
db.movies.updateOne( {title: {$eq: "Old School"}}, { $set: { "title": "New School", "releaseYear": 2010 } } )
Update release year of all the movies released in the year 2012 to 2014.
db.movies.updateMany( {year: {$eq: 2012}}, { $set: { "year": 2014} } )
Update movie with name “TitanicBig” with awardsWon to 9 and if it does not exists, add the movie with awardsWon value at 9.
db.movies.updateOne( {"title": {$eq: "TitanicBig"}}, { $set: { "title": "TitanicBig", "awardsWon": 9} }, {upsert:true} )
As I have used the keywords upsert, if mongodb update operation does not find the document with the movie name TitanicBig, it will create a new document for it.
I hope this example would give you an idea of how we can update documents (Updating Objects) inside MongoDB collection.
Learning
Here are some of the relevant blog posts on MongoDB.
- Foundations of Document Databases with MongoDB – Video Course
- SQL Terms vs MongoDB Terms
- MongoDB Compass – Missing a Schema Section
- MongoDB Fundamentals – Getting Started – Day 1 of 6
- MongoDB Fundamentals – CRUD: Creating Objects – Day 2 of 6
Technology Online
I hope you find these Learning paths helpful. If you have a Pluralsight subscription, you can watch it for free. If you do not have a Pluralsight subscription, you can still watch the course for FREE by signing up for a trial account. Please note that you do not need any credit card. You can always connect with me on twitter.
Reference: Pinal Dave (https://blog.sqlauthority.com)
First appeared on MongoDB Fundamentals – CRUD: Updating Objects – Day 4 of 6