Back to: MongoDB Tutorials
Working with MongoDB Database
In this article, I am going to discuss how to work with MongoDB Database i.e. how to Show, Create, and Drop MongoDB Database. Please read our previous article where we discussed How to Install MongoDB Database in macOS. At the end of this article, you will understand the following pointers.
- How to Show Databases in MongoDB?
- How to Create a new Database in MongoDB?
- How to Drop a Database in MongoDB?
- How to Create a Collection in MongoDB?
How to Show Databases in MongoDB?
MongoDB Database is a container that is used to store one or more collections and these collections are used to store documents. The documents contain the data in the form of fields with dynamic schema. A single MongoDB server can have multiple databases. When we install MongoDB on our system some database is automatically created. The default database of MongoDB is DB which is present in the data folder.
We can see all the database present on MongoDB using the show command. This command will return a list of all the databases present on the MongoDB server along with their size in gigabytes.
Note: Here we show the database using the mongo shell.
Command: show dbs
You can use these databases using the use command. This is how we can see all the databases present in the MongoDB server.
How to Create a Database in MongoDB?
MongoDB Database is a container that is used to store one or more collections and these collections are used to store documents. The documents contain the data in the form of fields with dynamic schema. A single MongoDB server can have multiple databases. When we install MongoDB on our system some database is automatically created. The default database of MongoDB is DB which is present in the data folder.
We can also create our own database using the use command. This command creates a new database if does not exist otherwise it will switch to the existing database. When you create a new database using the use command it will not display in the list of databases unless you insert at least one document in it.
Note: Here we create a database using the mongo shell.
Syntax: use Database_name
Naming Restrictions for Database in MongoDB:
Now we understand the naming restriction for the database:
- The names of the database are case insensitive but remember the names cannot differ only by the case of the character.
- For Windows, the database name should not contain any of the below characters in it: / \. “$*:|?
- For Unix and Linux, the database name should not contain any of the below characters in it: / \. “$
- For both windows and Unix and Linux, the database name should not contain null characters.
- The name of the database would not be empty and the names should be less than 64 characters.
Example to understand how to create a database in MongoDB:
First, we check the already present database using the following command:
Command: show dbs
Now we create our own database using the following command:
Command: use dotnettutorials
Now we check our newly created database in the list:
Command: show dbs
As we can see that dotnettutorials database is not present in the list because we do not add any document in it. So now we add a document in the dotnettutorials database using the following command:
Command: db.dotnettutorials.insert({“Name”: “Mohit”, “Age”:20})
Now we will again check the list of databases and see if our database is added to the list or not:
Command: show dbs
Now our database dotnettutorials is finally added to the list of databases. So, this is how we create a new database in the MongoDB server.
How to Drop a Database in MongoDB?
MongoDB Database is a container that is used to store one or more collections and these collections are used to store documents. The documents contain the data in the form of fields with dynamic schema. A single MongoDB server can have multiple databases. When we install MongoDB on our system some database is automatically created. The default database of MongoDB is DB which is present in the data folder.
In MongoDB, we are allowed to delete already present databases using dropDatabase() command. This command drops the existing database. It will delete one database at a time.
Syntax: db.dropDatabase()
Example to Understand How to Drop Database in MongoDB:
In the following example, we will discuss how to delete a database from the MongoDB server: First, we check the available database databases using the following command-
Command: show dbs
In the above list of databases, we want to delete “mydatabase”. So now we switch into “mydatabase” using the following command:
use mydatabase
And delete “mydatabase” using the following command:
db.dropDatabase()
Now we again check the list of databases. Now, you can see that mydatabase is not present which means it was deleted successfully.
This is how we drop a database from the MongoDB server.
How to Create a Collection in MongoDB?
In MongoDB, collections are used to store documents. A single database can store multiple collections in it and each collection is assigned with an immutable UUID. MongoDB Database is a schema-less database so it is not necessary that in a collection the schema of one document is the same as another document. Or we can say that a collection can hold different documents and each document is assigned with a unique id.
Naming Restriction on Creating a Collection in MongoDB:
Following are the naming restrictions for collections:
- The collection name should start with a character or an underscore.
- The name should not contain $
- The name should not be an empty string (””)
- The name should not contain null characters.
- The name should not begin with system.prefix
- The maximum length of the name is 120 bytes including the database name, dot separator, and collection name.
We can create a collection using the following methods:
Method 1 – Using createCollection() method
MongoDB provides a method named createCollection() which explicitly creates a collection in the specified database. This method provides various options like setting maximum size, validation rules, etc.
Syntax: db.createCollection(collectionName, options)
Parameters:
- collectionName: It is used to represent the name of the collection.
- Options: It is used to represent specific options about memory size and indexing. Some of the options are:
Example 1:
In the following example, we show how to create a collection in “dotnettutorials” database. We create a collection named “course” in the “dotnettutorials” using createCollection() method:
use dotnettutorials
db.createCollection(“course”)
Now we check the collection in the database using the show collections command:
Example 2:
In the following example, we show how to create a collection with an option. Now we create a capped collection by setting the value of the capped option to true using the following command:
db.createCollection(“mycapped”, {capped : true, size : 349585, max : 2000})
Method 2 – Adding a new document
We can also create a new collection by adding a document to it. It is the fasted method to create a collection. Here when you create a new document then MongoDB automatically creates a new collection.
Syntax: db.collectionName.insert({document})
Example:
In the following example, we will show how to create a collection without using createCollection() method. We add a document in the mycourse collection and MongoDB will automatically create a collection in the dotnettutorials database.
db.mycourse.insert({“name” : “Mohan”, “age”: 23})
Now we will check mycourse collection in the list of collections:
show collections
This is how we create a collection in the database.
In the next article, I am going to discuss How to Install MongoImport and MongoExport. In this article, I try to explain How to work with MongoDB Database. I hope you enjoy this how to Show, Create, and Drop MongoDB Database article.