Introduction to MongoDB and CRUD Operations

Oct 29, 2020 · 4 min read

How to use mongo shell in command line

If you haven’t installed MongoDB, or you want to check if it is running, you can read the previous article before reading this one.

After the installation, while MongoDB is running, you should be able to access mongo shell by just typing mongo in command line.

  1. Access the Mongo Shell:

Now, you should see “>” character in the beginning of the row.

In mongo shell you can type “help” to see some most common commands.

2. Help Command:

help command outcome

As you can see in the above table, we can request for the name of existing databases.

3. Show Database Names:

>show dbs

If you haven’t add any database yet, your result will be something like this:

existing databases

Let’s go ahead and create our first database. Use command allow us to create and also switch to that newly created database.

4. Create and Use a New Database:

use newDB

This command will also be useful when you want to switch among the existing databases. So again, if there is already a database called newDB, this command allow us to use it, instead of creating. If there is no database called newDB, then it creates a new one.

After creating a new database, when we type show dbs we don’t actually see the database we just created. That’s because for it to be listed, it has to have some content. But, still you can see the current database by typing db.

5. See Current Database


The outcome should be newDB if you run the command 4.

As I mentioned above we can’t really see the databases unless it has a content. Before we learn how to create new documents inside the database, it’s a good time to talk about CRUD.

Crud is a short version of saying four basic functions in storage operations. Namely, create, read, update, delete. To play with databases you should know these functionalities. Both in SQL and NoSQL environment you must use CRUD operations. There are small application differences though.

MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne( )
  • db.collection.insertMany( )

Collections in MongoDB are kind of similar to tables in SQL world. They are collections of documents. And documents are basically just an object, like a row in SQL based database.

Let’s see the create operations in real cases.

6. Insert One Method:

db.newCollection.insertOne({_id: 1, name: "New Doc"})

Firstly, you may ask where that newCollection came from. Just like we created newDB above, whatever you type in there, it will automatically created as a new collection. So, after this command we have our first collection: newCollection.

Using insertOne( ) method, you can insert only one document at a time. In this case the document we inserted is a simple JSON object. It has two fields; _id and name.

With same logic you can insert more than one document at a time with insertMany( ) method into the relevant collection. By the way you can check all the collections with following command.

7. See the Existing Collections

show collections

Read operations retrieves the document from a collection.

  • db.collection.find( )

That find( ) method has 2 optional parameters.

  • find(query, projection)

You can call it without any parameters and it will return the everything inside the collection. You need to add filters to get more specific results.

8. Find Method


Since we inserted only one document yet, we will get just that document as output.

9. Find Method with Query

db.newCollection.find({name: "New Doc"})

Now we are using query parameter and looking for a document that has a name “New Doc”.

10. Find Method with Query and Projection

db.newCollection.find({name: "New Doc"}, {_id: 1})

In this case we are using both query and projection parameters. Console will answer this command with a document which has a name “New Doc”. And it will project only the id field.

Pretty self-explanatory indeed. You can update the documents in your database with following commands.

  • db.collection.updateOne( )
  • db.collection.updateMany( )

With parameters:

  • db.collection.updateOne(filter, update)

This method can take more optional parameters. You can see all of them here. Head over to the real examples.

11. Update One Method

db.newCollection.updateOne({_id: 1}, {$set: {number: 5}})

First parameter is the filter parameter that we are looking for documents with id of 2. Then we create a new field using $set. Name of the new field is number and value is 5 which is just a random number. You can check it with find method.

Delete Operations

These operations allow us to remove documents from a collection. As usual, we have 2 methods:

  • db.collection.deleteOne( )
  • db.collection.deleteMany( )

12. Delete One Method

db.newCollection.deleteOne({_id: 1})

As you might imagine deleteMany( ) would remove all the documents that matches with the query.

Finally, if we want to delete the database we just created, there is a quick command for that. Before deleting, make sure that you are in the right database. You can type db to check the current database, and use newDB to switch.

13. Delete database


We no longer have newDB database.

Thanks for reading,

Take care,

The Startup

Get smarter at building your thing. Join The Startup’s +731K followers.