Nearby lessons

3 of 22

🗄️ MongoDB: Create & Drop Database and Collections

MongoDB provides commands to create, rename, and drop databases and collections. It also includes help commands to explore all available operations easily.

List all databases on the server

MongoDB Shell
NoSQL
Database
1show dbs
Query Result
JSON Output
admin 40.00 KiB
config 108.00 KiB
local 88.00 KiB
school

Switch to or create a new database

MongoDB Shell
NoSQL
Database
1use school
Query Result
JSON Output
already on db school

Delete the current database

MongoDB Shell
NoSQL
Database
1db.dropDatabase()
Query Result
JSON Output
{ "ok": 1, "dropped": "school" }

Create new collections

MongoDB Shell
NoSQL
Database
1db.createCollection("students");
2db.createCollection("library");
3show collections
Query Result
JSON Output
library
students

Rename a collection

MongoDB Shell
NoSQL
Database
1db.students.renameCollection("student_info");
2show collections
Query Result
JSON Output
library
student_info

Delete a collection

MongoDB Shell
NoSQL
Database
1db.student_info.drop();
2show collections
Query Result
JSON Output
library

Show available database operations

MongoDB Shell
NoSQL
Database
1db.help()
Query Result
JSON Output
Database Class:
getMongo Returns the current connection object.
getName Returns the name of the database.
getCollectionNames Returns an array of collection names.
getCollectionInfos Returns array of objects with collection info.
runCommand Runs an arbitrary command.
adminCommand Runs an admin command.
aggregate Runs an aggregation pipeline.
...

Show available collection operations

MongoDB Shell
NoSQL
Database
1db.student.help()
Query Result
JSON Output
Collection Class:
aggregate Calculates aggregate values.
bulkWrite Performs multiple write operations.
count Returns the count of documents.
countDocuments Returns the count matching query.
deleteMany Removes all documents matching query.
deleteOne Removes a single document matching query.
distinct Finds distinct values for a field.
estimatedDocumentCount Returns count of all documents.
find Selects documents in a collection.
findAndModify Modifies and returns a single document.
findOne Selects one document in a collection.
...
Keep Going!MongoDB - Insert Document