What are basic MongoDB commands and how to use them?

Last updated on Oct 07,2024 10.1K Views

What are basic MongoDB commands and how to use them?

edureka.co

MongoDB is trending right now. Starting from small scale startups, all the way to big organizations, everyone has started using it, therefore making this platform worth exploring. If you are new to the world of MongoDB and still getting the hang of using it, this article is for you. In this article, we will share the most popular MongoDB commands you can use on this platform to make your life easier and your coding process much more efficient.

Before we share the most popular commands of MongoDB, here is a small introduction to the platform. 

What is MongoDB?

MongoDB is an open-source relational database management system that was first introduced in the year 2009. It shares a lot of features with MySQL and brings to the table new enhancements and additional capabilities that contribute to its widespread popularity. 

Some of the companies which use MongoDb as their primary resource include the likes of HootSuite, Sony, and Zendesk just to name a few. 

Basic Commands of MongoDB

  1. Mongo: This is one of the most common commands used in MongoDB. When used, you are asking the platform to connect to localhost on default port 27017. 

  2. Mongo <host>/<database>: This command is used when you want the platform to connect to a particular database. An example of this command in action can be, mongo 10.121.65.58/mydb.

  3. Mongo –host <hostname or ip address> –port <port no>: If you want to connect to a remote host using a specified port, then you need to use this command. An example of this command in action can be, mongo –host 10.121.65.23 –port 23020.

  4. use <database name>: If at any point in time, you need to switch between existing databases, use this command. Example, use mydb.

  5. Db: If you need to view the current database you are using, make use of this command.

  6. Help: Similar to other platforms, MongoDB too comes with a built-in help window and in order to use it, run this command. Example, help

  7. load(<filename>): If you need to execute or run a JavaScript file at any point in time, make use of this command. Example, load (myscript.js).

  8. db.help(): If you need help with using Db methods, then you can make use of this command. Example, db.help().

  9. db.mycol.help(): If you need help using a collection, then you use this command. Example, db.mycol.help().

Show Commands

Now that you are aware of the basic commands you can use in MongoDB, here are some of the most popular show commands. 

  1. show collections: If you need to view all the collections in the current database, then make use of this command. Example: show collections.

  2. show dbs: In the midst of programming, if you need to view the current database being used then use this command. Example: show dbs.

  3. show roles: Within each database, there are different roles. In order to view all these roles, make use of this command. Example: show roles.

  4. jshow users: At any point in time, there can be multiple users on any database. In order to view all these users, make use of this command. For example: show users. 

CRUD Operations

CRUD in MongoDB is the industry wide accepted acronym for Create, Read, Update and Delete. As you know, read and write operations can be performed simultaneously in the MongoDB platform and in order to achieve it, use the following commands. 

  1. db.collection.insertMany([ <document1>, <document2>, … ]): If you need to insert multiple documents within an already existing collection, then make use of this command. Example, db.books.insertMany( [{“isbn”: 9780198321668, “title”: “Romeo and Juliet”, “author”: “William Shakespeare”, “category”: “Tragedy”, “year”: 2008}, {“isbn”: 9781505297409, “title”: “Treasure Island”, “author”: “Robert Louis Stevenson”, “category”: “Fiction”, “year”:2014}]). 

  2. db.collection.insert( <document> ): If you need to insert a single new document into an already existing collection, then make use of this command. Example, db.books.insert({“isbn”: 9780060859749, “title”: “After Alice: A Novel”, “author”: “Gregory Maguire”, “category”: “Fiction”, “year”:2016}). 

  3. db.collection.find(<query>): If you need to find a specific document within a collection by using a field value condition, then use this command. Example, db.books.find({“title”:”Treasure Island”}). 

  4. db.collection.find(): If you need to find all the documents in an already existing collection, then make use of this command. Example, db.books.find(). 

  5. db.collection.findOne(<query>, <projection>): If you need to find the first document that matches the query you have given, then make use of this command. Example: db.books.findOne({}, {_id:false}). 

  6. db.collection.find(<query>, <projection>): If you need to find some specific fields of a document in a collection, then you can make use of this command. Example: db.books.find({“title”:”Treasure Island”}, {title:true, category:true, _id:false}). 

  7. db.collection.update(<query>, <update>): If you need to remove certain in an existing document, by matching a query then you can make use of this command. Example: db.books.update({title : “Treasure Island”}, {$unset : {category:””}}). 

  8. db.collection.update(<query>, <update> ): If you need to update some specific fields of a document that match the given query, then make use of this command. Example: db.books.update({title : “Treasure Island”}, {$set : {category :”Adventure Fiction”}}). 

  9. db.collection.remove(<query>, {justOne:true}): If in a certain situation, you need to delete a single document that matches your query then use this command. Example: db.books.remove({title :”Treasure Island”}, {justOne:true}). 

  10. db.collection.update(<query>, <update>, {multi:true} ): If you need to delete certain fields of all the documents that match your query, then use this command. Example: db.books.update({category : “Fiction”}, {$unset : {category:””}}, {multi:true}).

  11. db.collection.remove({}): If you need to delete all the documents in a collection, irrespective if they match your query or not, then use this command. Example: db.books.remove({}).

  12. db.collection.remove(<query>): If you need to delete all the documents that match a certain query, then make use of this command. Example: db.books.remove({“category” :”Fiction”}). 

Conclusion

Similar to other relational database management systems, MongoDB too contains a lot of commands that come in handy in day to day use. Depending upon your use case, make use of any or all of the commands shared above. 

If you wish to learn Microsoft SQL Server and build a career in the relational databases, functions, and queries, variables etc domain, then check out our interactive, live-online SQL Training Course here, which comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Mention them in the comments section and we will get back to you or join Mongodb certification course online. 

Upcoming Batches For MongoDB Certification Training Course
Course NameDateDetails
MongoDB Certification Training Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Advanced Data Modeling with Power BI and Azure