Basic MongoDB Commands List For Beginners

MongoDB is a popular NoSQL database known for its flexibility & ease of use. If you are new to MongoDB, understanding the basic commands is crucial to efficiently managing your databases. In this blog, we'll cover essential MongoDB commands, including how to show databases, list databases, create users, create databases, drop databases, & other basic commands. This guide will help you get started with MongoDB & become more comfortable with its command-line interface.

Show Database in MongoDB

One of the first things you might want to do is see a list of all the databases in your MongoDB instance. To do this, you can use the show dbs command. This command lists all the databases along with their sizes.

show dbs

When you run this command, MongoDB will display a list of databases and their sizes. This is useful for quickly checking what databases exist on your server.

MongoDB List Databases

Similar to the show dbs command, the listDatabases command in MongoDB provides a detailed list of all databases. This command is executed within the MongoDB shell and provides more detailed information.

db.adminCommand('listDatabases')

This command not only lists the databases but also provides additional details like the total size on disk.

MongoDB Show Databases

To show databases in MongoDB, you can use the show databases command. This is synonymous with the show dbs command and works the same way.

show databases

Both commands will provide you with a list of all the databases available on your MongoDB server

MongoDB Create User

Creating a user in MongoDB is essential for managing access and ensuring security. To create a user, you need to be connected to the database where you want to create the user and then use the createUser command.

use admin
db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "exampleDB" }]
})

This command creates a user with the specified username and password and assigns them the readWrite role on the exampleDB database.

MongoDB Create Database Command

In MongoDB, databases are created implicitly. When you switch to a non-existing database and insert data, MongoDB will create the database for you. However, you can also explicitly create a database by switching to it and then creating a collection.

use newDatabase
db.createCollection("newCollection")

This command creates a new database called newDatabase and a collection named newCollection.

MongoDB Basic Commands

Here are some basic commands that every MongoDB beginner should know:

Insert Data:

To insert a document into a collection.

db.collectionName.insert({ key: "value" })

Find Data:

To query documents from a collection.

db.collectionName.find({ key: "value" })

Update Data:

To update documents in a collection.

db.collectionName.update({ key: "value" }, { $set: { key: "new_value" } })

Delete Data:

To delete documents from a collection.

db.collectionName.remove({ key: "value" })

These commands are fundamental for interacting with your data in MongoDB.

MongoDB Drop Database

If you need to delete a database, you can use the dropDatabase command. This command will permanently remove the database and all its data.

use databaseName
db.dropDatabase()

Make sure you are connected to the database you want to drop before running this command.

MongoDB Commands List

To summarize, here's a list of the essential MongoDB commands covered in this blog:

  • Show Databases: "show dbs" or "show databases".
  • List Databases: "db.adminCommand('listDatabases')".
  • Create User: "db.createUser()".
  • Create Database: "use newDatabase"followed by"db.createCollection()".
  • Insert Data: "db.collectionName.insert()".
  • Find Data: "db.collectionName.find()".
  • Update Data: "db.collectionName.update()".
  • Delete Data: "db.collectionName.remove()".
  • Drop Database: "db.dropDatabase()".

Conclusion

Understanding these essential MongoDB commands will help you manage your databases more effectively. Whether you need to show databases, create users, or drop a database, these commands provide a solid foundation for working with MongoDB. As you become more familiar with these commands, you will find managing your data in MongoDB to be straightforward and efficient.

By frequently practicing these commands, you'll gain confidence and be well on your way to mastering MongoDB. Happy coding!

Press ESC To Close