Top Mongodb Commands You Need to Know
This Article Discusses the Most Useful Commands for Mongodb Database Administration. We’ll First Get Familiar with the Basic Concepts of Mongodb. Then, I’ll...
This article discusses the most useful commands for MongoDB database administration.
We’ll first get familiar with the basic concepts of MongoDB. Then, I’ll show you how to carry out a variety of basic administrative functions, with commands for:
- Connecting
- Viewing databases, collections, roles & users
- Managing users
- Checking logs
- Managing the database
- Gathering collection details
- Renaming collections
- Terminating an instance
Let’s get started!
(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)
MongoDB overview
MongoDB is a high performance, highly scalable cross-platform NoSQL database. MongoDB relies on concepts like Documents, Collections, and Databases:
- Collections and documents are analogous to the traditional table and rows in an RDBMS database.
- A single MongoDB instance can contain multiple databases. The database is a physical container for collections with a dedicated file structure in the system.
A Collection contains a group of MongoDB documents. Collections are created within a database and do not enforce a schema like a traditional database. Therefore different documents in the same collection can have different fields. MongoDB Documents are based on key-value pairs.
Let’s take a quick look at how traditional RDBMS terminology relates to MongoDB structure.
| Relational Database Management System | MongoDB |
| Database | Database |
| Table | Collection |
| Row | BSON document |
| Column | BSON field |
| Index | Index |
| Primary key | _id field (Primary key) By default, MongoDB auto-generates a 12-byte hexadecimal number for each document. |
| Group | Aggregation |
| Join | Embedding and linking |
Like any database, MongoDB needs administration. That’s where administrative commands come in—let’s take a look.
Must Read
Commands for connecting to MongoDB
First, we need to know how to connect to a MongoDB database. You can use the mongo command to connect with a MongoDB database and use parameters like host and port if needed.
- mongo Run this command in the localhost shell to connect to the local database on the default port 27017.
- mongo <host>/<database> Specify the host and database as parameters to connect to a specific database.
- mongo –host <hostname/IP> –port <port no> [options] You can use this format to specify different options while connecting the database. Refer to the mongo man pages or help for details information about all available options.
mongo --host 10.10.10.59 --port 27017 --verbose
mongo –host <hostname/IP> –port <port no> –authenticationDatabase <database> -u <user> -p <password>
If authentication is enabled in the MongoDB installation, we can specify the user details and the authentication database. The authentication database is where the user details reside, which can be any database that is used to create users. If you don’t give the password in the command, cmd will ask for it later.
mongo --port 27017 --authenticationDatabase "admin" -u "barryadmin" -p