Database
In MongoDB, a database contains the collections of documents. One can create multiple databases on the MongoDB server.
View Database:
To see how many databases are present in your MongoDB server, write the following statement in the mongo shell:
show dbs
For Example:
Here, we freshly started MongoDB so we do not have a database except these three default databases, i.e, admin, config, and local.
Naming Restriction for Database:
Before creating a database you should first learn about the naming restrictions for databases:
- In MongoDB, the names of the database are case insensitive, but you must always remember that the database names cannot differ only by the case of the characters.
- For windows user, MongoDB database names cannot contain any of these following characters:
/\. "$*:|?
- For Unix and Linux users, MongoDB database names cannot contain any of these following characters:
/\. "$
- MongoDB database names cannot contain null characters(in windows, Unix, and Linux systems).
- MongoDB database names cannot be empty and must contain less than 64 characters.
Creating Database:
In the mongo shell, you can create a database with the help of the following command:
use database_name
This command actually switches you to the new database if the given name does not exist and if the given name exists, then it will switch you to the existing database. Now at this stage, if you use the show command to see the database list where you will find that your new database is not present in that database list because, in MongoDB, the database is actually created when you start entering data in that database.
For Example:
Here, we create a new database named w3wiki using the use command. After creating a database when we check the database list we do not find our database on that list because we do not enter any data in the w3wiki database.
MongoDB – Database, Collection, and Document
Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server. A Database contains a collection, and a collection contains documents and the documents contain data, they are related to each other.
Contact Us