25 Most Common Commands for MongoDB Beginners

25 Most Common Commands for MongoDB Beginners
25 Most Common Commands for MongoDB Beginners

Introduction

Many IT firms have displaced their previous tech stack with MongoDB, and are hiring Mongodb developers at lucrative packages. Even real-time systems such as clocks, traffic lights are being driven by software paradigms, with this the dire need for a real-time database was acknowledged and the demand for MongoDB boosted up significantly.

What is MongoDB?

Introduced in 2009, MongoDB is a widely used open-source relational database management system. A bundle of MongoDB features is similar to MySQL. On top of this, MySQL adds more to the table manipulating functions and preserving capabilities that highly contribute to its spiking demand. 

The document data model adopted by MongoDB is highly efficient for storing and retrieving data that allows swift relay of information. MongoDB’s horizontal, scale-out architecture can back up huge volumes of both data and traffic. Many top-notch companies with an extended client base have adopted MongoDB as their prime database management system, such as HootSuite, Sony, and Zendesk.

How to install MongoDB?

  • Download the MongoDB installer file.
  • Find the downloaded .msi file in Windows Explorer. Double click the file and follow the prompts to install Mongo. 

Note: unless you specify a custom directory Mongo is by-default installed in the C:\mongodb directory**. Furthermore,you might alter the directory name based on your requirement.

  • Create a new directory where MongoDB files should be stored. From the command prompt run md \data\db. This location comes by default. Moreover, other locations can be declared using the –dbpath parameter. Refer to the Mongo documentation for further details.
  • Initiate the MongoDB daemon by running C:\mongodb\bin\mongod.exe in the Command Prompt. Alternatively, by running, C:\path\to\mongodb\bin\mongod.exe.
  • Connect to MongoDB using the Mongo shell, even when the MongoDB daemon is running, from a varied Command prompt window run C:\mongodb\bin\mongo.exe

Five common commands for MongoDB beginners

MongoDB comes with a set of commands that can be run on the command prompt for swift manipulation of data. Let’s discuss the most popular commands used in MongoDB:

Basic MongoDB commands

  • Mongo: This is the most popularly used command in MongoDB. If this command is triggered, you trigger the platform to connect to localhost on default port 27017. 
  • Mongo <host>/<database>: You can type this command in case you wish a platform to connect to a specific database. A demo of this command in practice can be, mongo 10.121.65.58/mydb.
  • Mongo –host <hostname or ip address> –port <port no>: If the user intends to connect to a remote host using a specified port, then he needs to take the help of this command. For example, mongo –host 10.121.65.23 –port 23020.
  • use <database name>: If at any instance, you require to toggle between existing databases, employ this command. Example, use mydb.
  • Db: If you wish to display the current database you are using, try using this command.
  • Help: In coherence with other platforms, MongoDB also comes with a default help window and in order to use it, run this command. Example, help
  • load(<filename>): If you wish to execute or run a JavaScript file at any instance, take the help of this command. Example, load (myscript.js).
  • db.help(): If you need any assistance with respect to Db methods, then you can simply take help from this command. Example, db.help().
  • db.mycol.help(): If you need any help with respect to, then you use this command. Example, db.mycol.help().

Show Commands

  • show collections: In case you need to display all the collections in the current database, type this command in the command prompt. For example show collections.
  • show dbs: If you are amidst programming and you need to view the current database being availed then you might use this command. For example: show dbs.
  • show roles: There are different roles within a database. In case you need to view all these roles, you can use this command. For example: show roles.
  • jshow users: At any instance, there can be more than one user on any database. In case you intend to view all these users, make use of this command. For example: show users. 

Did You Know You Can Create a URL Shorter using MongoDB & Node

CRUD Operations

CRUD in MongoDB is the thoroughly recognised acronym for Create, Read, Update and Delete. As we are already aware, read and write operations can be carried out parallelly in the MongoDB platform and in order to carry it out effectively, we take the help of the following commands.

  • db.collection.insertMany([ , , … ]): If you intend to insert more than one document within an already existing collection, you can use this command. For 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}]).
  • db.collection.insert( ): In case you have to insert a single new document into a pre-existing collection, then take assistance from this command. Example, db.books.insert({“isbn”: 9780060859749, “title”: “After Alice: A Novel”, “author”: “Gregory Maguire”, “category”: “Fiction”, “year”:2016}).
  • db.collection.find(): If you need to find a particular document within a collection with the help of a field value condition, then go for this command. Example, db.books.find({“title”:”Treasure Island”}).
  • db.collection.find(): If you require to find all the documents in an already existing collection, then assistance from this command. Example, db.books.find().
  • db.collection.findOne(, ): If you need to find the first document that aligns with the query you have been given, then take assistance from this command. Example: db.books.findOne({}, {_id:false}).
  • db.collection.find(, ): If you are required to find specific fields of a document in a collection, then take assistance from this command. For example: db.books.find({“title”:”Treasure Island”}, {title:true, category:true, _id:false}).
  • db.collection.update(, ): For removing certain elements in an existing document, by matching a query you can make use of this command. For example: db.books.update({title : “Treasure Island”}, {$unset : {category:””}}).
  • db.collection.update(, ): In case you need to update a few particular fields of a document that matches a given query, then make use of this command. For example: db.books.update({title : “Treasure Island”}, {$set : {category :”Adventure Fiction”}}).
  • db.collection.remove(, {justOne:true}): If in a specific scenario, you have to delete a single document that matches your query then use this command. For example: db.books.remove({title :”Treasure Island”}, {justOne:true}).
  • db.collection.update(, , {multi:true} ): If the user intends to delete certain fields of all the documents that match the query, then use this command. For example: db.books.update({category : “Fiction”}, {$unset : {category:””}}, {multi:true}).
  • db.collection.remove({}): If you need to delete all the documents in a collection, even if they match your query or not, then go for this command. For example: db.books.remove({}).
  • db.collection.remove(): If you need to delete all the documents that match a given query, then select this command. For example: db.books.remove({“category” :”Fiction”}).

Frequently Asked Questions

Which command is used to run MongoDB?

C:\mongodb\bin\mongod.exe command is used for running MongoDB. Alternatively, you can also use C:\path\to\mongodb\bin\mongod.exe.

What is DB in MongoDB commands?

The in-built database of MongoDB is ‘db’, which is located inside a data folder. MongoDB can create databases as and when required. It is not necessary to create a database prior to you starting working on it. The “show dbs” command is used for displaying the list of all the current databases.

How do I view data in MongoDB?

collection. find() command is used for automatically iterating the cursor to display up to the first 20 documents. Use it to further continue the iteration. For accessing the returned documents within a driver, use the specific cursor handling mechanism with respect to the driver language.

How do I show collections in MongoDB?

To view the list of MongoDB collections, you need to type the Mongo shell command show collections in the command prompt . It returns all collections created inside the MongoDB database. It is mandatory to pick a database where at least one collection is present.

What is MongoDB collection?

A collection is referred to as grouping of MongoDB documents. Documents within a collection might possess different fields. A collection is analogous to a table in a traditional relational database system. Collections don’t enforce a schema. A collection is present inside a single database. (teamtapper.com)

How do I connect to MongoDB?

For connecting your local system to MongoDB, you must set the Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections, unless edited. Click on connect, for viewing the databases in your local MongoDB.

Key Takeaways

Concurrently to the various other relational database management systems, MongoDB also comes with a wide set of commands that come in handy for day to day use by MongoDB beginners. According to your use case, you might employ one or all of the MongoDB commands mentioned in the list above.  

Level Up In Your Career With Our Premium Courses | Enrol Now

MongoDB is around a hundred times more efficient than the conventional database system such as RDBMS, which is not that efficient when compared to other NoSQL databases. MongoDB is also backed by deep query-ability, that is, you are allowed to perform dynamic queries on documents using the document-based query language which is at par with SQL.

By Vanshika Singolia