MongoDB is a popular, open-source NoSQL database that provides high performance, high availability, and automatic scaling. As a document-based database, it stores data in flexible, JSON-like documents, making it easy to work with and integrate into various applications. In this article, we’ll cover the fundamentals of creating and dropping databases in MongoDB, allowing you to confidently manage your data.
Table of Contents
- Prerequisites
- Understanding MongoDB Structure
- Creating a MongoDB Database
- Listing Databases
- Dropping a MongoDB Database
- Conclusion
1. Prerequisites
Before diving into creating and dropping databases, ensure that you have MongoDB installed on your system. You can download it from the official MongoDB website. Additionally, it is helpful to have a basic understanding of JSON, JavaScript, and the command line.
2. Understanding MongoDB Structure
MongoDB organizes data into databases, collections, and documents. A database contains multiple collections, which are analogous to tables in a relational database, and each collection holds documents. Documents are BSON (Binary JSON) objects that store data in a flexible, semi-structured format.
3. Creating a MongoDB Database
In MongoDB, you don’t explicitly create a database. Instead, you create a database by switching to a non-existent database, then inserting data into it. The new database is created automatically when you insert the first document.
To create a database, follow these steps:
- Open the MongoDB shell by running the mongo command in your terminal or command prompt.
- Use the use command followed by the desired database name to switch to the new database:1use myNewDatabase
- If the database does not exist, MongoDB will create it automatically. To verify that you’ve switched to the new database, use the db command:1db
- To insert data into the new database, create a collection and insert a document:1db.myCollection.insertOne({ key: "value" })
Replace myCollection with the desired collection name and { key: “value” } with a valid BSON document. After executing this command, MongoDB will create the new database and collection, and store the document.
4. Listing Databases
To list all databases in your MongoDB instance, execute the show dbs command in the MongoDB shell:
1 | show dbs |
This command will display a list of databases along with their sizes. The newly created database should appear in the list.
5. Dropping a MongoDB Database
To drop a database, follow these steps:
- Open the MongoDB shell by running the mongo command in your terminal or command prompt.
- Use the use command followed by the target database name to switch to the database you want to drop:1use myExistingDatabase
- To drop the current database, execute the db.dropDatabase() command:1db.dropDatabase()
MongoDB will delete the specified database, including all of its collections and documents. Note that this operation is irreversible, so use caution when executing it.
Conclusion
By mastering the process of creating, listing, and dropping databases in MongoDB, you gain the foundational knowledge needed to manage your data effectively. As you continue to explore MongoDB, you’ll discover its various features and capabilities, allowing you to build powerful and scalable applications.
4 Comments
Which software we should use to execute mongodb for Android
All MONGODB QUERIES SHOULD BE PROPERLY GIVEN ALL THE TIME .HELP STUDENTS.
Can you tell me where do I have to write those comands please?
Use mongo shell https://tecadmin.net/tutorial/mongodb/mongo-shell/