How to Connect, Create Databases, and Building Collections with Mongosh in MongoDB

How to Connect, Create Databases, and Building Collections with Mongosh in MongoDB

Managing a MongoDB database can be a rewarding experience due to its flexibility and scalability. Here are a few key points to consider:

Basic Commands

  • Connect to MongoDB: Use the mongosh command to connect to your MongoDB instance. Here’s a basic command to connect to a local MongoDB instance running on the default port (27017):
  • Create a Database: You don’t need to explicitly create a database in MongoDB. It will be created automatically when you first store data in it.
  • Create a Collection: Similarly, collections are created when you insert documents into them.

Mongosh

Connecting to mongosh (MongoDB Shell) is quite straightforward. Here’s how you can do it:

  • Open Terminal or Command Prompt: Open your terminal (macOS/Linux) or Command Prompt (Windows).
mongosh
mongosh output

Once connected, you can start using mongosh commands to interact with your MongoDB databases.

Show dbs;

To show the databases in MongoDB, you can use the following command in the MongoDB shell:

show dbs

This command will list all the databases available in your MongoDB instance along with their sizes. Here’s an example output:

Note-These are the default databases. Please do not use them for security reasons.

admin       0.000GB
config      0.000GB
local       0.000GB

Each database name is followed by the size of the data it contains.

Create Database

Creating a database in MongoDB is straightforward because MongoDB automatically creates a database when you first store data in it. Here’s how you can create a database manually:

  • syntax is use <database_name>
use students

Create Collection

Creating a collection in MongoDB is simple. Here’s how you can do it using this command:

  • syntax is db.createCollection.(‘<collection_name>’)
db.creatCollection('data') // in my case data is collection name

Show Collection

To show collections in a specific database using the shell, you can use the following command:

show collections 

This command will display a list of all collections in the selected database. For example:

Insert Data into a Collection

db.data.insertOne({name: "anil kumar", age: 30})

This data of above command is stored in MongoDB database

Deleting databases and collections

Deleting databases and collections in MongoDB is straightforward. Here are the steps:

  • Switch to Your Database: Use the use command to switch to your desired database. In my case my database name is students.
use students
  • Drop the Collection: Use the drop method to delete the collection.in my case collection name is data.
db.data.drop()
  • This command will remove the collection named data from the database students.

Deleting a Database

  • Drop the Database: Use the dropDatabase method to delete the database.
db.dropDatabase()

This command will remove the entire database students.

Always be careful when dropping collections and databases, as this action is irreversible and will permanently delete your data.

Happy Coding !

Leave a Reply

Your email address will not be published. Required fields are marked *