There are many GUI tools to connect to MongoDB databases and browse but sometimes you need to get to the command line. That moment when something has gone wrong on the database server, and you need to SSH 4-levels deep in order to debug a problem with a database can leave you stuck.
In this cheat sheet you can get a quick refresher on:
- Starting the mongo shell
- Navigating around Mongo
- Working with a collection
- Changing groups of documents
- Working with individual documents and Indexes
- Dangers to avoid
MongoDB Commands Listing (Common Line Options):
There are numerous options provided by MongoDB but in this section, let us discuss the most common options of MongoDB when used in conjunction with the MongoDB shell startup, to work with MongoDB database server seamlessly.
S.No. | Option | Description |
---|---|---|
1 | –help | Lists all the available options that can be used while starting up the MongoDB Shell. |
2 | –nodb | Specifies to start the MongoDB shell connecting to any database. |
3 | –shell | Specifies to start the shell after running any specific *.js files earlier. |
4 | –version | Specifies the version information of the MongoDB shell during startup. |
5 | –quiet | Starts the MongoDB shell with not many chatty messages. |
MongoDB Commands Listing (Command Helpers):
There are various command helpers available for the MongoDB shell (mongo). The table below talks about the most commonly used help commands.
S.No. | Help Commands and Commands | Description |
---|---|---|
1 | help | Shows help related information on the MongoDB shell. |
2 | db.help() | Shows help related information on the database methods. |
3 | db..help | can be an existing collection or not, but provides help related information on database collections. |
4 | show dbs | Lists all the databases available for use on the connected MongoDB instance. |
5 | show databases | Lists all the databases available for use on the connected MongoDB instance. |
6 | use | Specifies the MongoDB shell to switch to the database provided with the parameter, switches the shell parameter (db) to it. |
7 | show collections | Lists all the collections available for use on the current database. |
8 | show users | Lists all the users available on the current database. |
9 | show roles | Lists all the roles (both built-in roles and user-defined roles) on the current database. |
10 | show profile | Lists the last 5 recent operations that took 1 millisecond or more. |
11 | load() | Executes a specified javascript file. |
MongoDB Commands Listing (Basic Shell JS Operations):
Apart from the above help options and command-line options, MongoDB provides a rich collection of Javascript API for database related operations. DB is the mongo shell variable that holds the current database that we are pointing to. The variable is reset to the when the command is used, until then it points to the test database.
Some of the commonly used Javascript operations are discussed in the table below.
S.No. | JS Database Operations | Description |
---|---|---|
1 | db.auth() | Authenticates the user, if the MongoDB shell is running in the secure mode. |
2 | myCollectionVariable = db. | Assign a specific collection ‘myCollection’ to the variable ‘myCollectionVariable’, as shown in the example below:myCollectionVariable = db.myCollection;You can then perform operations on myCollection on the variable myCollectionVariable instead. See below for example |
3 | db.collection.find() | Finds all the documents in a specific collection and returns a cursorFrom the example above, myCollectionVariable.find() |
4 | db.collection.insertOne() | Inserts one document into the collection specified via the command. |
5 | db.collection.insertMany() | Inserts multiple documents into the collection specified via the command. |
6 | db.collection.updateOne() | Updates one single document in the collection specified via the command. |
7 | db.collection.updateMany() | Updates multiple documents in the collection specified via the command. |
8 | db.collection.save() | Inserts a new document into the collection if it doesn’t exists, and if it exists updates the document in the collection. |
9 | db.collection.deleteOne() | Deletes one document from the collection specified via the command. |
10 | db.collection.deleteMany() | Deletes multiple documents from the collection specified via the command. |
11 | db.collection.drop() | Drop or remove the collection entirely from the database |
12 | db.collection.createIndex() | This command creates a new index on the collection specified via the command, if it doesn’t exist. If the index already exists, then there is no effect of this command over the collection specified by the command. |
Database
Description | Command |
---|---|
show all database | show dbs |
show current database | db.getName() |
switch(and create if doesn’t exit) to database ‘userdb’ | use usersdb |
drop current database | db.dropDatabase() |
Collection
Description | Command |
---|---|
show all the collection under current database | db.getCollectionNames() |
create collection ‘users’ | db.createCollection(“users”) |
drop collection ‘users’ | db.users.drop() |