Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIHow to Create Database & Collection in MongoDB

How to Create Database & Collection in MongoDB

Introduction

MongoDB is a NoSQL database solution that focuses on availability and scalability. The data is structured and stored in collections of JSON documents.

By not using a fixed data structure, MongoDB provides a comprehensive solution for high-volume storage in a modern distributed system.

This tutorial shows you how to use the MongoDB client to create a new database.

how to create database in mongodb in ubuntuhow to create database in mongodb in ubuntu

Prerequisites

  • Access to terminal window/command line
  • Elevated user privileges (sudo, root, or administrator)
  • A working MongoDB installation (Centos 8 or Ubuntu)
  • A Linux or Windows system

Create MongoDB Database with the use Command

MongoDB commands are issued within the MongoDB client shell.

1. To access the MongoDB shell, open a terminal window, and run the following command:

mongo

You are now in the MongoDB shell and can start issuing database commands.

2. Start by issuing a use command. The use command is typically used to switch to a specific database. However, MongoDB creates a new database if one does not exist.

use example_db

3. The system immediately responds and switches to your new database.

example of the Use command creating a new database in mongodbexample of the Use command creating a new database in mongodb

You are currently using your newly created database named example_db.

Note: If you can’t access the MongoDB shell, check whether the MongoDB service is active with sudo systemctl status mongodb. The output should confirm that the service is active (running). For further details see Managing MongoDB Service.

How to List Databases in MongoDB

1. Verify the name of the current database by using the db command:

db

Your system should display example_db (or the name you specified).

example of using the db command checking database nameexample of using the db command checking database name

2. List all the databases on your system with the show dbs command:

show dbs

You may notice that your new database is not listed.

show dbs command Listing of MongoDB databasesshow dbs command Listing of MongoDB databases

The newly created database is empty – you’ll need to insert data for it to display in this list.

Add MongoDB Collection with insert() Command

A collection in MongoDB is much like a table in other database applications.

The following command creates a record and inserts it into a newly created collection. A record is a document with field names and values. As with a new database, if this record does not exist, MongoDB creates it:

db.Sample.insert({"SampleValue1" : 255, "SampleValue2" : "randomStringOfText"})

Let’s take a closer look at the syntax of the command to understand how the record and collection are created.

  • db.Sample.insert – This is the insert command. In this example, the name Sample represents the name of the document you’re inserting data into. If the document does not already exist, MongoDB automatically creates it.
  • ({  }) – The set of brackets encloses the data you are entering. Note that there’s a set of close-brackets at the end.
  • "SampleValue1" : 255, "SampleValue2" :  "randomStringOfText" – Represents the format of the data we are entering into the record.

After you execute the command, you see the following response:

WriteResult({ "nInserted" : 1})

This response confirms that the system automatically created a collection and the record was inserted into said collection.

MongoDB confirms that you have successfully inserted data to a new collectionMongoDB confirms that you have successfully inserted data to a new collection

If you list the existing databases with the show dbs command, you’ll notice that the example_db database is now on the list.

New example_db database is visible on database list.New example_db database is visible on database list.

Query MongoDB with find Command

You can verify that MongoDB saved the data from the insert command using the find command:

db.Sample.find()

As a result, the system displays the data located within the Sample collection.

find command prints information within the collectionfind command prints information within the collection

Conclusion

Use the commands in this article to create, display, and insert data into your MongoDB database.

MongoDB offers countless ways to structure your data and many commands to help you along the way.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments