In this post, you will learn about MongoDB commands which could get you started and perform minimum database related activities such as create, update, drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as the cheat sheet. You may want to bookmark this page for quick reference.
The following is the list of the commands:
sudo service mongod start sudo service mongod stop
mongo --host localhost:27017
show dbs
use testdb
Until a collection is created in a database, the database name is not listed as a result of execution of the command, “show dbs”
db.createCollection("user")
show collections show tables
The following command also work:
db.getCollectionNames()
db.user.insert({"name": "Ajitesh Shukla", "location": "hyderabad", "username": "eajitesh"})
db.user.find() db.user.find().pretty()
db.user.find({"username": "eajitesh"}) db.user.find({"username": "eajitesh", "location": "hyderabad"})
db.user.drop()
db.createUser({"user": "ajitesh", "pwd": "gurukul", "roles": ["readWrite", "dbAdmin"]})
show users
mongo -u USERNAME -p PASSWORD --authenticationDatabase DATABASENAME
For user created in above command, the login command would look like the following:
mongo -u ajitesh -p gurukul --authenticationDatabase testdb
In this post, you learned about the MongoDB cheatsheet commands (especially, helpful for beginners) which could help you quickly get started with MongoDB.
Did you find this article useful? Do you have any questions or suggestions about this article? Leave a comment and ask your questions and I shall do my best to address your queries.
In this fast-changing world, the ability to learn effectively is more valuable than ever. Whether…
Last updated: 13th May, 2024 Whether you are a researcher, data analyst, or data scientist,…
Last updated: 12th May, 2024 Data lakehouses are a relatively new concept in the data…
Last updated: 12th May 2024 In this blog, we get an overview of the machine…
Last updated: 12th May, 2024 In the world of generative AI models, autoencoders (AE) and…
Last updated: 7th May, 2024 Linear regression is a popular statistical method used to model…