How to Organize Data in MDB ?
Overview
MongoDB stores the data in the form of documents which are stored inside the collection and collections are grouped inside the databases.
Introduction
Document oriented structure is used by MongoDB for processing, managing, and storing the data. Collections are stored in the database and collections consist of the documents. As static schema is not used for defining every document structure, more flexibility is provided by the document-based system in comparison to relational systems in which data is stored in the form of records and tables.
How to View Existing Databases
The show dbs command is used for displaying all the available databases of the system that are allowed to be accessed by you.
Output:
On running this command all the accessible database is displayed by the MongoDB along with the current storage space information of every database/ When you check on which database you are currently active and working on, write the command db.getName(), or simply db can also be used.
Output:
Sometimes the database name not displayed in the output of the show dbs command is given as the currently active database. This happened because the database was not created until the first document creation of that database.
The use command along with the database name is used to change the currently operating database.
Output:
The db.stats() command can be used for getting the current active database basic information.
Output:
The output of this command displays the total number of collections available in the database, information of the index, storage statistics of the database, etc.
How to Create Databases
There is no explicit command available in MongoDB for database creation. In place of that, we need to specify while storing the documents that we want to store in the new database. The database is created implicitly at the time of the creation of the first document of that database. For preparing the MongoDB for document creation in the new database, the use command needs to be written to change the current database to a non-existent database.
Here is an example of creating a new database with the name database1
Output:
If now the db command is run to check the currently active database, then the output of that command confirms that the database with the name database1 is created.
But the database is not created until the creation of documents in that database.
Output:
For the actual creation of the new database, you need to write some documents into that particular database.
How to View the Collections in a Database
Documents can be grouped by collections according to our categorization system. Collections are available in the databases and documents can be stored in these.
show collections is executed to display all the available collections of the currently active database.
Here we are changing the database to the admin database.
Output:
The db.getCollectionNames() method can be used if you want to get the name of the same collection in the form of an array. But this method is deprecated starting from MongoDB version 4.0. So in place of this, you can use the db.listCollections() method to get the names of the collections in the current database:
Output:
The db.getCollectionInfos() method is used to display the extra info regarding the collection in the present database.
Output:
Particular collection names can also be passed in the command for the result filtering.
Implement the db.<collection>.count() method so that you can check the number of documents available in the collection. For example, we can write the following command for checking the number of documents available in the collection named system.version.
Output:
The db.printCollectionStats() method is used for displaying the information of basic statistics of the collections available in the currently active database.
How to Create Collections
Collections can be created in two ways: collections can be created explicitly or implicitly.
Collections are created automatically by MongoDB at the time of the first document creation of that collection. It tells the MongoDB for the new collection created by document insertion into that collection which does not yet exist.
For example, we are changing the database to the database1 which we used earlier. New documents can be inserted into the collection by the .insertOne() command after changing the current database. Here we are inserting a document in a new collection named collection1.
Output:
The output shows that the document insertion is completed successfully. Three actions are performed by the execution of the above command. Firstly a database with the name database1 is created and then the collection with the name collection1 is created inside that database. And finally, a document is inserted in the collection by the insertOne() command. Collection can be created explicitly by the db.createCollection() method. This allows the creation of a collection without inserting any document inside it.
For example, if you want to create a new collection in the database1 with the name collection1 then you have to write the following command.
Output:
Collection creation can be verified by the show collections command, and collection2 is displayed in the list of collections.
Output:
How to Delete Collections
For collection deletion, we can use the drop() method on that particular collection.
For example, a collection with the collection1 can be dropped by writing the command
Output:
The operation was completed successfully and can be tested by listing all the collections of the currently active database.
How to Delete Databases
The db.dropDatabase() command is used for whole database deletion.
Output:
The currently active database is deleted by this db.dropDatabase() command.
Output:
After deleting the database, if we run the show dbs command to test the database deletion then the database1 is not displayed in those databases.
Output:
But as we have not yet switched to the new database, MongoDB is still set up for the creation of the database1 database when we choose to insert a new document or new collection. db command can be executed to test this.
Output:
Conclusion
- MongoDB stores the data in the form of documents which are stored inside the collection and collections are grouped inside the databases.
- show dbs command is used for displaying all the available databases of the system that are allowed to be accessed by you.
- use database_name command is used to change the currently operating database.
- db.stats() command can be used for getting current active database basic information.
- db command is run to check the currently active database.
- show collections is executed to display all the available collections of the currently active database.
- Collections can be created in two ways: collections can be created explicitly or implicitly.
- db.dropDatabase() command is used for whole database deletion.
- For collection deletion, we can use the drop() method on that particular collection.