Introduction to MongoDB

Introduction to MongoDB

·

3 min read

MongoDB is an open-source NoSQL document-oriented database that is designed to hold a lot of data and provide you with a lot of flexibility when working with it. NoSQL databases are used in place of conventional relational databases. Working with sizable distributed data sets makes good use of NoSQL databases. MongoDB is a technology that can manage, store, and retrieve information that is document-oriented. When analysing real-time data across various development environments, MongoDB simply converts JSON and JSON-like documents, such as BSON, into Java objects, making reading and writing data in MongoDB exceptionally quick and effective.

Prerequisites:

Make sure the following prerequisites are installed before you start:

Basic Commands:

Running MongoDB:

After completing the prerequisites, In your command prompt enter the command mongosh . For dealing with MongoDB deployments, the MongoDB Shell, also known as mongosh, is a fully working JavaScript and Node.js environment. To test queries and actions on your database directly, use the MongoDB Shell.

In your command prompt, if the installation is successful you will see all the details.

MongoDB Help:

In the MongoDB client, use db.help() to get a list of commands. You will receive a list of commands as seen in the screenshot below.

Show All Databases:

Use show dbs to get a list of all databases.

Create a new Database:

To create a new database, execute the following command- use DATABASE_NAME

  • Note: If a database with the same name already exists, use DATABASE_NAME will switch to that database right away. If not, a new database with the same name is created and switched over immediately.

Know your current selected database:

Run the following command db to find out which database is currently active or selected.

Create a Collection:

To create the new collection execute the following command
db.createCollection(name)

To check all Collections:

To get the list of collections created execute the following command
show collections

Insert document in Collection:

Run the following command to insert a single document into the chosen collection.

db.COLLECTION_NAME.insert(document)

  • To insert multiple documents in the selected collection execute the following command db.COLLECTION_NAME.insertMany(document)

Get all documents in a Collection:

Execute the following command to acquire a list of the documents in a collection: db.COLLECTION_NAME.find()

Update Document:

To update the document in the collection execute the following command
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

  • You can also use, db.COLLECTION_NAME.updateOne(SELECTION_CRITERIA, UPDATED_DATA) for updating one document.

  • Update many documents in a collection by using,db.COLLECTION_NAME.updateMany(SELECTION_CRITERIA, UPDATED_DATA) . This method updates all the documents in MongoDB collections that match the given query.

Delete Document:

  • To delete a document in the selected collection execute one of the following commands db.COLLECTION_NAME.remove(DELLETION_CRITTERIA) , db.COLLECTION_NAME.deleteOne(DELLETION_CRITTERIA) .

  • To delete all documents in one command, you can use db.COLLECTION_NAME.deleteMany({})

Drop a Collection:

To drop the selected collection execute the following command
db.COLLECTION_NAME.drop()

Drop Database:

Execute the following command to delete the database, which will delete the chosen database.
db.dropDatabase()

Conclusion:

In this article, we have explored the basic commands that are used in MongoDB, to store datasets. Once you get familiar with the basics, you are ready to move ahead and connect your web applications with the database. We walked through setting up the database , creating collections, storing documents, and performing essential operations such as creating collections, inserting datasets in documents, updating documents, and deleting documents/collections. This is the beginning of the NoSQL database and various basic operations in them. There is a lot more to learn./

Happy Coding!