MongoDB Dev and Admin (17 Blogs) Become a Certified Professional
AWS Global Infrastructure

Databases

Topics Covered
  • Cassandra (14 Blogs)
  • MongoDB Dev and Admin (15 Blogs)
  • MySQL (55 Blogs)
  • SQL Essentials Training and Certification (9 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What are basic MongoDB commands and how to use them?

Published on Nov 13,2019 83 Views
What are basic MongoDB commands and how to use them?

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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. 

Got a question for us? Mention them in the comments section and we will get back to you. 

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.