Getting Started with MongoDB a NoSQL database using the MongoDB Shell: A Beginner's Guide

Getting Started with MongoDB a NoSQL database using the MongoDB Shell: A Beginner's Guide

Table of contents

No heading

No headings in the article.

What are NoSQL databases?

A NoSQL database is a type of database management system that stores data in an unstructured manner and it doesn't require a clearly defined schema to store data. They're used in situations where large amounts of data need to be stored e.g E-commerce applications, Content Management Systems, Customer Relationship Management etc.

In this article, I would love to enlighten you about a particular NoSQL database known as MongoDB.

MongoDB is a database management system that uses the document-based data model, where data are been stored in form of JSON or BSON.

A record in MongoDB is made up of key-value pairs where the values can be of different data types e.g strings, numbers, booleans, arrays, dates or nested documents.

Practical aspect

For this section, I would be working with the MongoDB command line interface also known as the MongoDB shell(mongosh).

NB: it is a must to have MongoDB installed locally on your machine and the MongoDB server needs to be running on your machine.

I would be making use of the MongoDB Query Language (MQL) to perform most actions.

Creating a database:

To create a new database in the shell type the command "use (database name)", then hit the enter button to switch to that database. In the picture below Sample is been used as the database name.

To see the list of databases, use the command "show dbs". A list of databases is been returned with the amount of data contained in them.

Collections

A collection in MongoDB is defined as a grouping of documents.

To create a MongoDB collection use the "db.createCollection()", where db stands for the current database and the database we want our collections to be stored. The picture below "person" was the name given to the collection.

A database can contain more than one collection. To see a list of collections available in the database run "show collections".

Performing basic CRUD operations

CRUD operation - The acronym stands for Create, Read, Update and Delete operation.

Create:

To create a new document in MongoDB, use the db.person.insertOne().

where db stands for the database, person is the collection in the database where we want the current document to be stored and insertOne({document}) represents the method we want to carry out, it is used to store just one document in form of JSON.

There's also another method known as the insertMany() method used in cases where there's a need to store more than one document at once.

Reading:

This operation has to do with getting data from the MongoDB database. To get all the documents in a particular collection run db.person.find({}), and all the documents in the collection get to be returned. The empty braces({}) stand for all documents in the collection. In cases where you want some documents to be returned, you can use this same method but some conditions would be passed into the braces. For example db.person.find({"age": 17}), this query gets all the documents in the database where the age is 17.

Getting a particular document from the database, run the query db.person.findOne({pass a condition associated with the document needed}). In the picture below the condition given is the document which has the name value "John_Doe".

Update:

Updating documents has to do with changing some things in them. To carry out the update operation use the query db.person.updateOne() If the updating is for only a document or db.person.updatemany() If it is multiple documents.

The method takes in two parameters, where the first stands for the document to be updated and the second is the change to be carried out on the document. In the second parameter, there's an operation there known as the set operation, used to replace the value of a field with a specific value.

Delete:

There might be a need to delete either a document or all the documents in a collection and to do this you either run the query db.person.deleteOne({}) to delete a particular document or

db.person.deleteMany({}) to delete all the available documents in the collection. Once the operation has been carried out a response is been sent which has the number of deleted documents.

To find out more about the MongoDB database, you can check out their course here: MongoDB Documentation