In MongoDB, you don't explicitly create a database like you would in a relational database system. MongoDB is a NoSQL database, and databases and collections (equivalent to tables in relational databases) are created automatically when you insert data into them.
To work with a specific database, you can use the use command. For example, to switch to a database named "user," you can type.
Here are the basic steps to create a database in MongoDB:
use user
In MongoDB, you can list all the databases by using the show dbs command
In the MongoDB , simply type the following command and press Enter:
Verify Database Creation: You can check if the database and collection have been created by running:
show dbs
Remember that in MongoDB, a database is only created when you insert data into it or perform some operations that require the database to exist. You don't need to explicitly create a database schema as you would in a traditional relational database. Just start inserting data into the desired database, and MongoDB will create it if it doesn't exist.
Switch to the Database: Use the use command to switch to the database where you want to create the collection. Replace "user" with the name of your target database:
Syntax
db.createCollection("collectionName")
Example
db.createCollection("student")
If the collection is successfully created, MongoDB will acknowledge it with a { "ok" : 1 } response.
Verify Collection Creation: You can verify that the collection has been created by listing all the collections in the current database using the show collections command
show collections
You have now created a collection named "collections" within the "user" database. You can start inserting documents into this collection and performing various operations on it.
Keep in mind that this command only lists collections in the currently selected database. If you want to see collections in a different database, you'll need to use the use command to switch to that database and then run show collections again.
To delete a database in MongoDB, you can use the db.dropDatabase() method in the MongoDB.
Warning: Deleting a database in MongoDB is a permanent operation, and all data in the database will be lost. Be cautious when using this command, and make sure you have a backup if necessary.
Once you are in the correct database, run the following command to delete it:
db.dropDatabase()
MongoDB will prompt you with a warning message to confirm the deletion. Type 1 or true to confirm and delete the database:
{ok:1, dropped:'student'}
Please exercise caution when using the dropDatabase command, as it permanently removes all data in the specified database, and there is no way to recover it. Make sure you have a backup of your data if needed before proceeding with the deletion.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions