MongoDB Databases and Collections

Creating a Database

In MongoDB, databases are created implicitly when you first store data in them. You can create a new database using the use command in the MongoDB shell:

use databaseName

Collections

Collections are analogous to tables in relational databases. They store documents (similar to rows in a table) with dynamic schemas.

To create a new collection, you can insert a document into it:

db.collectionName.insert({name: "John Doe", age: 30})

Replace collectionName with the desired name for your collection.

To drop (delete) a collection, use the drop() method:

db.collectionName.drop()

Dropping a Database

To drop (delete) a database, first, switch to the database you want to drop:

use databaseName

Then, use the dropDatabase() command:

db.dropDatabase()

Last updated

Was this helpful?