CRUD operations in MongoDB
See more tutorials in MongoDB. This post has No Comments.
Introduction
The web is constantly evolving and as it evolves, we are beginning to see traditional SQL based databases being replaced by document object databases like MongoDB. The advantages these databases have often begins with agility and scalability, however these aren’t the only redeeming qualities that databases like MongoDB possess. Today we’ll be explaining these advantages alongside the basic CRUD operations of MongoDB.
For the purpose of this tutorial we’re using the MongoDB interactive shell, if you don’t know what that is, or need help installing it you can find it here.
Create
Our first operation of discussion will be creating objects within the database. MongoDB uses a JSON style object format called BSON as it’s storage. BSON is pretty much a binary representation of a JSON object. So it makes sense that the methods that are enacted upon the database are also JavaScript-esque. We’re going to be creating a users collection and our first user. A collection in MongoDB is synonymous with a table in SQL based databases.
Let’s take a look:
1 |
db.users.save({name:"John Doe",age:30,job:"engineer",email:"name@example.com"}) |
Above we’re using the MongoDB save() method to save our data to our users collection.
Read
Now that we have a data object in our database, we can read it! There are many ways to read data in MongoDB. The primary method for reading data is the find() method. Let’s take a look at some basic usage of the find() method.
1 |
db.users.find({name:"John Doe"}) |
In the above statement, we’re querying the database using the find() method with the parameter of {name:”John Doe”}. It’s important to remember that the find() method will return all documents in the database that mach the key value pair.
We can also manipulate our query by using query cursor methods.
To return only the first document that matches we need to use the findOne() method:
1 |
db.users.findOne({name:"John Doe"}) |
If you’re just trying to figure out how many documents match your query, you can use the count() method:
1 |
db.users.count({name:"John Doe"}) |
Update
To update a document in your MongoDB database you need to use the update() method. The update method accepts two parameters, the query and the update. The query is the first parameter passed through the method, then the update parameter which is preceded by a comma.
An example of the update() method looks like this:
1 |
db.users.update({name:"John Doe"}, {age: 28}) |
Be cautious, this will overwrite everything in your document and leave it as just “{ age: 28 }”.
To update just a single key value pair, we need to add some keywords to the query:
1 |
db.users.update({name: "John Doe"}, { $set: { age: 28}}) |
Now we would retain all of the information that was in the document, and just change the value of age.
For more options and information regarding the update() method, head over to the MongoDB documentation.
Delete
The last method is pretty simple, it’s the remove() method. This method is what you would use to delete data or sets of data from your database:
1 |
db.users.remove({name: "John Doe"}) |
You could use keywords to alter the meaning of the remove method from within the parameter parenthesis, for more on this visit the documentation.
Like the find() method, this method will delete every document that matches or contains the query parameter unless otherwise specified.
Those are the basics of creating, reading, updating and deleting with MongoDB. As you can see it’s very agile and easy to learn!