Getting to Know the Common MongoDB Commands
In this post, we'll break down and study the most popular commands that can be used on mongoDB databases and if you want to learn how to create databases on mongodb using different methods, try checking out our article about it here. Managing MongoDB databases with these commands will help you setup and become comfortable with the mongoDB database platform.
To get a list of commands, type
in MongoDB client.
Log Into MongoDB
To log into the MongoDB database, run the following command.
Check if the user with the credentials username and password exists in the database specified in place of dbname.
Show All Databases
Once signed in as a user with the proper role, such as userAdmin or userAdminAnyDatabase, the user may view all databases by using the command
Select Database to Work With
To begin working with a specific database, run the following command:
Authenticate and Log Out From Database
When using the use dbName command to switch to a different database, the user must authenticate using a valid database user for that database. For authentication, run the following command:
If you're done with your work with the database and want to logout, Just simply type:
List Down Collections, Users, Roles
To check existing collections, users, and so on, use the following commands.
List down collections of the current database
List down all the users of all current database
List down all the roles
Create a Collection
To make a collection, use the following command. This page contains further information on this command if you want a more in-depth guide on this.
Insert a Document in a Collection
After creating a collection, the following step is to input one or more documents. A example command for inserting a document into a collection is shown below.
Insert single document
Insert multiple documents
Save or Update Document
Depending on the document parameter supplied to it, the save command can be used to either edit an existing document or insert a new one. If the _id given is the same as that of an existing document, the document is updated. If not, a new document is produced. The insert or update command is used internally by the save mechanism.
If a matching document is located, it is modified; if none is found, a new document is produced.
Display Collection Records
The following commands can be used to retrieve collection records:
Retrieve all records
Retrieve limited number of records; Following command will print 10 results; if you're looking for a larger record, simply change the number
Retrieve records by id
Retrieve values for certain collection attributes by giving an object with attribute names set to 1 or 0 depending on whether the attribute value should be included in the output or not.
The administrative commands listed below can help you find collection details such as storage size, total size, and overall statistics.
Get the collection statistics
Latency metrics for read and write operations, including average read and write times and the number of operations executed
Get collection size for data and indexes
Size of the collection
The total size of documents stored in the collection
The total size of all indexes in the collection
Connect to MongoDB database
Once you've downloaded and installed MongoDB, you can use the mongo shell to connect to an active MongoDB server. Note: Your server must be operational before you connect to it using the shell. You can start the server in CMD using the following command:
Then type the mongo command to run the shell.
Now you are in the Mongo shell.
You can launch mongo and mongod without using the command prompt if you choose. To do so, navigate to the installation directory and double-click the mongod and mongo apps. You will obtain the same result as in the previous example.
The above mongo command will only function if your MongoDB server is listening on port 27017. If your MongoDB server uses a different port, you must mention it explicitly in the command, as demonstrated below:
Both of the above instructions are only valid if your MongoDB server is operating on localhost. Use the '-host' argument with the mongo command to connect to a remote server, as demonstrated below.
Starting out your database journey in any platform can be difficult, but with the right research you can master and harness these commands to your will and help you manage your databases easier! Trying out Automated Backups like Ottomatik can be a huge help especially if you're managing a huge amount of projects and will save you from major headaches if an accident happens!