mongodbMongoDB is one of the “NoSQL” types of database solutions used to store and query big data. Old SQL developers might find Mongo a bit counterintuitive. With normal, relational databases, you create a database, then tables and then insert your records. MongoDB and NoSQL databases are completely different. In fact, you don’t need to create a database in Mongo. Mongo automatically creates the database when you insert a new collection, so technically there is no “create database” command when you’re working with NoSQL.

What is NoSQL?

Before you get started with MongoDB, you should understand some of the differences in NoSQL. It can be difficult to get started when you’re used to the old-school relational SQL queries you’ve used for several years.

NoSQL uses the concept of collections. You no longer need primary keys, foreign keys and relationships. Instead, you have collections and each collection can store whatever you want. This makes NoSQL extremely versatile and flexible. However, if the database is managed wrong or you aren’t sure how to properly create and link your data, you can actually make a much slower interface than with a relational database.

When you insert a collection or record, MongoDB gives it an ID. This object ID uniquely identifies the record among your other collections.

Creating a Database

If you do a little research to find out how to create a database in MongoDB, you’ll find documentation for the function “db.createCollection()”. This is actually how you create a database, but it’s referred to as a collection in NoSQL. The following code is the format for the collection object and function:

db.createCollection(name, options)

For instance, suppose you want to create a database named “Customers” to store customer information. You would then use the following code:


Notice the above code just creates the collection, but the options are left out. These options are optional for the collection command, but you might want to include a size for the database or even a maximum size. The following code is an example of setting a max size of 64KB:

db.createCollection(“Customers”, max: 64 * 1024)

Notice the 1024 is added, because the value is set in bytes. One issue to note when setting a size or a maximum size. When the size or maximum size is reached, MongoDB will start overwriting older documents. A document is a record in MongoDB, so this could be a major issue with setting database sizes.

Once you’ve created your collection, you probably want to verify that it was created even if you receive no errors when you run the command. In the MongoDB console, type “show dbs.” This will show you all databases on the server. In this case, you should see the “Customers” collection.

Maybe you want to switch to a database or even change the database name. Here is where NoSQL is similar to other relational databases. Type “use Customers” to switch to the new customers database and make it the active collection. One issue to note with the “use” command is that even if the database doesn’t exist, MongoDB will make it. At this point, the database or collection isn’t made if it hasn’t been created yet. It’s created once you create a document and store it in the active database.

For instance, suppose you type the following into Mongo:

use customerr

Now, you accidentally mistyped the name of the Customer database. MongoDB still makes the mistyped database active, and as soon as you create a record, the collection is created. This can be a problem if you’re not careful, because you will go back looking for the “Customers” collection and its records and you won’t find them.

Interestingly, the collection isn’t created right away. If you type “show dbs” you will only see the “Customers” collection and not the mistyped “customerr” collection. This is one trick to be careful of when working with MongoDB’s console command interface.

Adding a Document to Your Collection

Now that you have your collection (database), you can add documents. Documents are the same as records if you are learning from a relational database background. The main difference between a table record and a document is the lack of restraints set in NoSQL.  You can add a document that contains a certain amount of data and then the next document contains a different set of information. This is the flexibility power with MongoDB and NoSQL databases in general.

For instance, using our “Customers” collection, you probably want to add a user name to the collection. This is the user name the customer will use to log in to your system. The following code shows you how to save a new document to your collection: {username:”myuser”} )

Notice there is a huge difference between your typical relational database engine and MongoDB. There is no INSERT statement and no columns specified. It’s just a list of key-value pairs, similar to what you would see in other languages such as JSON.

Also not that there is no ID specified, but an ID is created for it. Type the following command into the MongoDB console:


Look at the results. Notice that there is an “ObjectId” section that contains an alphanumeric result. This is the document’s ID that uniquely identifies the record.

Let’s go back to the issue with the mistyped “customerr” collection. Let’s say we activated the wrong database and inserted this new customer document into the activated collection. Type “list dbs” in the console and notice that now the mistyped “customerr” database has been created. This is the power and the disadvantage of MongoDB and NoSQL in general. It’s extremely flexible but it lets you make more mistakes than a relational database.

You probably want to remove the record if you’ve accidentally stored it in the wrong collection. Luckily, MongoDB includes a remove function. This function is similar to the delete function you’re used to in relational databases.  The following is the basic command syntax for the remove runction:

db.Customers.remove({ })

Within the brackets is what you need to use to specify the criteria for the removal. In this example, we only have one record with a username key-value pair, so we can use it. The following code is how you could remove the document we inserted:

db.Customers.remove({ username: “myuser” })

The above code removes any document that contains a username key-value pair with the value “myuser.” Since you only have one record with this match, MongoDB will then remove that record from the database.

This type of remove probably isn’t very useful when you have large amounts of documents. Just like a relational database, you want to limit any accidental deletions by removing documents using the ID value. Since this record is unique to just one document, you’re sure to delete only one record from the database. The following code shows you how to remove just one record using the ID:

db.Customers.remove( {“_id”: ObjectId(“<id>”)});

Replace “<id>” with the ID of the record you reviewed with the “find” function earlier.

These commands are just a basic overview of the NoSQL and MongoDB functionality. If you’re used to SQL and relational databases, it will take some time to get used to the new commands. Source


Subscribe to our Newsletter

Get The Free Collection of 60+ Big Data & Data Science Cheat Sheets. Stay up-to-date with the latest Big Data news.