DEV Community

iamanand07
iamanand07

Posted on

Getting Started With MongoDB

Before getting started with MongoDB we have to know

  • what really a database is,
  • Why we use databases,
  • What are the different types of databases are available,
  • How to choose the database that suits our requirements.

WHAT IS A DATABASE?

A Database is a collection of tables which have the information stored in rows and columns, in which we can do whatever CRUD(CREATE,READ,UPDATE,DELETE) operations we need.
simple right!!

WHY WE USE DATABASE?

If we want to store large amount of data we need to have a database which is acts as a container to store our data in a understandable manner.

What are the different types of databases are available?

The following are the different types of databases available,

  • Centralized database.
  • Distributed database.
  • Personal database.
  • End-user database.
  • Commercial database.
  • NoSQL database.
  • Operational database.
  • Relational database.
  • Cloud database.
  • Object-oriented database.
  • Graph database.

To know more about types of databases

How to choose the database that suits our requirements

For choosing a database for our project we have to think about five important aspects they are

  • Integration.
  • Scaling Requirement.
  • Support Consideration.
  • CAP Consideration(Consistency, Availability, and Partition tolerance).
  • Schemas or Data Model.

INTRODUCTION TO MONGODB

A MongoDB is a container for all the collections, where Collection is a bunch of MongoDB documents similar to tables in RDBMS and Document is made up of the fields similar to a tuple in RDBMS, but it has a dynamic schema here.

Image description

  • MongoDB is a NoSQL cross-platform document-oriented database.
  • MongoDB is one of the popular databases which is written in C++.
  • MongoDB stores data as BSON documents.
  • BSON is a binary representation of JSON documents.
  • It organized as Database->Collections->Documents.

BASIC OPERATIONS PERFORMED IN MONGODB

  1. Create Database.
  2. Drop Database.
  3. Create Collection.
  4. Drop Collection.
  5. Insert Document.
  6. Query Document.
  7. Update Document.
  8. Delete Document.

1. Create Database.

To create a new database in mongodb we can use the command
use DATABASE_NAME. The command will create a new database if it doesn't exist, otherwise, it will return the existing database.

>use student
switched to student
Enter fullscreen mode Exit fullscreen mode

Here we have created a database student with use command COOL!!

2. Drop Database.

To drop a existing database in mongodb we can use the command
db.dropDatabase(). The command will remove a database if it does exist.

>use student
switched to student
>db.dropDatabase();
{ "ok" : 1 }
Enter fullscreen mode Exit fullscreen mode

First we have entered into the database then we can use dropDatabase command to delete our existing database.

3. Create Collection.

To create collection in MongoDB , we can use db.createCollection(name, options)
In this command, name is the name of the collection to be created. options are a document and are used to specify the configuration of collection.

>db.createCollection("newCollection")
{ "ok" : 1 }
Enter fullscreen mode Exit fullscreen mode

We can see list of collections by show collections command.

4. Drop Collection.

To drop a existing collection in mongodb we can use the command
drop(). The command will remove a collection if it does exist.

>db.newCollection.drop()
true
Enter fullscreen mode Exit fullscreen mode

Here newCollection is the name of the collection. true is the value that returned that the collection is dropped successfully.

5. Insert Document.

There are two ways for inserting a document in mongodb.

  • Inserts a single document.
  • Inserts multiple documents.
  1. To insert a single document we can use db.collection.insertOne().It inserts a single document into a collection.
> db.newCollection.insertOne({
    title: 'My first Blog',
    description: 'Insert a single document to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85
 });
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5e5972397596f4d696a78944")
}
Enter fullscreen mode Exit fullscreen mode
  1. To insert multiple documents we can use db.collection.insertMany().It inserts multiple documents into a collection.It will pass an array of documents to the method.
db.newCollection.insertMany([
   {
    title: 'My first Blog',
    description: 'Insert a single document to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85
   },

   {
    title: 'My second Blog',
    description: 'Insert multiple documents to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85


   }
]);

{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5e5972397596f4d696a78944"),
                ObjectId("5e7572397455f4d696a78597")
        ]
}
Enter fullscreen mode Exit fullscreen mode

6.QUERY DOCUMENT.

To retrieve the document that we just inserted, query the collection:

>db.newCollection.find().pretty();
{
    "id": ObjectId("5e5972397596f4d696a78944")
    "title": 'My first Blog',
    "description": 'Insert a single document to a collection',
    "by": 'Anand',
    "url": 'https://dev.net',
    "tags": [
             'NoSQL',
             'database', 
             'beginner'
],
    "likes": 85
 }
Enter fullscreen mode Exit fullscreen mode

find() method will display all the documents in a non-structured way.
pretty() method will display all the documents in a understandable manner.

7. Update Document.

There are three ways to update a document in mongodb , they are

db.newCollection.updateOne(<filter>, <update>, <options>)
db.newCollection.updateMany(<filter>, <update>, <options>)
db.newCollection.replaceOne(<filter>, <update>, <options>)
Enter fullscreen mode Exit fullscreen mode

8.DELETE DOCUMENT.

There are three ways to delete a document in mongodb , they are

db.newCollection.deleteOne() - Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.
db.newCollection.deleteMany() - Delete all documents that match a specified filter.
db.newCollection.remove() - Delete a single document or all documents that match a specified filter.

Top comments (0)