Contact Me

Mongodb tutorial

MongoDB is an open source database that uses a document-oriented NoSQL database system. In this system all of data found in a document and each document has been multiple collections and all data store in the collections.

Mongodb tutorial

Note: If do not have install mongodb yet, please flow the link For Mac, For Windows and For Linux

Note: If not cleared the instruction below, Please see the image beside.

Mongodb database create

  1. Open your terminal for Mac or CMD for Windows.
  2. Run mongo server by the command mongod
  3. Open another terminal for run the database and type mongo and press enter.
  4. After successfully run your database type db.testdb.insert({}).
  5. Then you can see the result like :WriteResult({ "nInserted" : 1 }).
Mongodb database create

Create collections in mongodb

  1. First go to your new document which you create just before by type the commanduse testdb
  2. Create a colections by the command db.createCollection("info")
  3. Now see the document by the command show dbs.
  4. And see the collections inside the document type use testdb and then show collections.
Create collections in mongodb

Mongodb insert data

Mongodb Insert One

See the code below how to insert just one document in mongodb database using insertOne methode.

db.info.insertOne({
    "name": "Imran",
    "email":"imran@gmail.com",
    "phone":"01751000000"
})
Mongodb insert data

Mongodb Insert Many

See the code below how to insert multiple document together in mongodb database using insertMany methode.

db.info.insertMany([
    {
    "name": "Rahim",
    "email":"rahim@gmail.com",
    "phone":"01751000001"
    },
    {
    "name": "Karim",
    "email":"karim@gmail.com",
    "phone":"01751000002"
    },
])
mongoDBdocument

Mongodb get data

In mongodb collections generally we can able to see data using several methods like find(), findOne() etc. Let's now see some example for get or read data.

Mongodb Find

Find all data into the collections without pretty() function.

db.info.find()
Mongodb get data

Mongodb Find All With Pretty Function

Find all data into the collections with pretty() function

db.info.find().pretty()
Mongodb Find All

Mongodb Find one

If you do like to find just first one data, Please flow the function below(No need to use pretty())

db.info.findOne()
Mongodb Find one

Find just particular field in Mongodb

If you want to see just particular field into the collections, you have need to use find() or findOne() methode and you have also need to specified field which you want to see from the collections. Lets see the some example below.

db.info.findOne({},{name:1, email:1, _id:0})
or
db.info.find({},{name:1, email:1, _id:0})
or
db.info.find({name:"Karim"},{name:1, email:1, _id:0})
mongoDBdocument

Mongodb Update :

Set a new field in Mongodb existing colections just one

See the function below how to set a new field in mongodb existing colections. By this function below you can able to set just one new field into the collection.

db.info.updateOne({
    image: {$exists: false}
},{
    $set: {
        image: "your image link"
    }
})
Mongodb Update

Set a new field in Mongodb existing colections all

See the function below how to set a new field into the mongodb existing colections which is the field are missing.

db.info.updateMany({
    image: {$exists: false}
},{
    $set: {
        image: "your image link"
    }
})
mongoDBdocument

Set a new object field in Mongodb existing colections

How to set an object field into the exesting mongodb collections. It's will be work for one or all both inserting the exesting collections. Just define updateOne or updateMany methode.

db.info.updateOne({
    phone: "01751000000"
},{
$set: {
    "awards":{
        "wins": 10,
        "nominations": 14,
        "text": "Imran and Salma are two do work togather."
    }
}
});
mongoDBdocument

Set a new array field in Mongodb existing colections

How to set an array field into the exesting mongodb collections. It's will be work for one or all both inserting the exesting collections. Just define updateOne or updateMany methode.

db.info.updateOne({
    phone: "01751000000"
},{
    $set: {
        subject:[{
            name: "Math1",
            code : "4410",
            cradit : "3"
        }]
    }
})
mongoDBdocument

Update data into Mongodb existing colections

How to update the field into the exesting collections.

db.info.updateOne({
    name: "Rahim"
},{
    $set: {
        image: "image link is updated",
        email:"update_imran@gmail.com"
    }},{
        upsert: true
})
mongoDBdocument

Update data into array field

How to update the mongo array field into the exesting collections.

db.info.update({
    "subject.name": "Math1"
},{
    $set: {
        subject:[
            {
                "name" : "Math2",
                cradit: "3.5",
                code:"4400"
            }
        ]
    }},{
        upsert: true
})
mongoDBdocument

Insert data into array field

How to insert new data into the mongo array field

db.info.update(
    {name:"Rahim"},
    {$push:{
        subject:{
            $each:
            [
                {
                    name:"Math2",
                    cradit:"3",
                    code:"1212"
                }
            ]
        }
    }
})
insert-into-array

Update the Mongodb object field

How to update the mongo object field into the exesting collections.

db.info.update({
    "awards.wins": 10
},{
    $set: {
        awards:{
                "wins" : 12,
                "nominations": 5,
                text:"dumy text update.."
            }
    }},{
        upsert: true
})
mongoDBdocument

Mongodb increment and decrement value

How to increment and decrement value into mongo data.

db.info.update(
    {"awards.wins": 27},
    {$inc: {"awards.wins" : -15, "awards.nominations" : 15}}
)
mongoDBdocument

Mongodb Delete query

Mongodb remove just one field

How to remove just one field into the mongodb collections.

db.info.updateOne({
    image: {$exists: true}
},{
    $unset: {
        image: ""
    }
});
Mongodb Delete query

Mongodb remove all particular field into existing collections

How to remove all of the existing field into the collections

db.info.updateMany({
    image: {$exists: true}
},{
    $unset: {
        image: ""
    }
});
mongoDBdocument

Mongodb remove the array data into the collections

How to remove data into the mongo array

db.info.update({
    name: "Rahim"
},{
    $pull: {
        subject:{
            code:"1212"
        }
    }
});
mongoDBdocument

Mongodb remove a full object into the collections

How to remove a full object into the collections

db.info.remove({name:"Salim"})
mongoDBdocument

Contact

Md. Al Imran Islam
(Software Engineer)

imran.meanstack@gmail.com

meanstackfundamental.com

Al-imran

01751888673