Skip to main content

Mongo Shell

note

All the commands listed here can be used in any Mongo application. For example, in a Node.js project.

Basic Shell Commands

CommandDefinition
mongoshOpen a connection to a local MongoDB instance
show dbsShow all databases in the current MongoDB instance
use <db_name>Switch to the selected database by it's name db_name
dbShow current database name
show collectionsShow all collections in the current database
db.dropDatabase()Delete the current database
clsClear the terminal screen
exitExit the current session

CRUD operations commands

note

Each one of these commands is run on a specific collection. Meaning we have to select a collection first in order to execute te command.

db.<collectionName>.<command>

Create

Read

Update

Delete

deleteOne

Delete the first document that matches the filter object.

$ db.users.deleteOne({ age: 10 })

deleteMany

Delete all documents that matches the filter object

$ db.users.deleteMany({ age: 15 })

Filter and Queries