Friday, December 29, 2017

Quick Start : MongoDb on Ubuntu 17.04

MongoDb Installation:

Install MongoDb (with super user)
    apt install  mongodb-server

Status of mongodb service
    systemctl status mongodb
    systemctl restart mongodb
        or
    service mongodb start
    service mongodb stop
    service mongodb status

Auto Start service
    systemctl enable mongodb

Version Check
    mongod --version

Configuration File:
    /etc/mongodb.conf

Getting Started with Mongo:

Start Mongo CLI:
  mongo

Insert JSON:
    db.mycollection.insertMany([
    {fname:"john",mname:"joe",lname:"doe",skill:["c#","javascript","sql"]},
    {fname:"mary",mname:"joe",lname:"doe",skill:["ccna","ccnp","cci","operation research"]},
    ])

View Inserted Json:
        db.mycollection.find({}).pretty();
    Output:
        {
            "_id" : ObjectId("5a4634cf26837e3337746881"),
            "fname" : "john",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "c#",
                "javascript",
                "sql"
            ]
        }
        {
            "_id" : ObjectId("5a4634cf26837e3337746882"),
            "fname" : "mary",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "ccna",
                "ccnp",
                "cci",
                "operation research"
            ]
        }
Filter Inserted Json on single property:
        db.mycollection.find({"fname":"john"}).pretty();
    Output:
        {
            "_id" : ObjectId("5a4634cf26837e3337746881"),
            "fname" : "john",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "c#",
                "javascript",
                "sql"
            ]
        }

Filter with contains:
        db.mycollection.find({skill:{$in:["c#","ccna"]}})
    output:
        { "_id" : ObjectId("5a4634cf26837e3337746881"), "fname" : "john", "mname" : "joe", "lname" : "doe", "skill" : [ "c#", "javascript", "sql" ] }
        { "_id" : ObjectId("5a4634cf26837e3337746882"), "fname" : "mary", "mname" : "joe", "lname" : "doe", "skill" : [ "ccna", "ccnp", "cci", "operation research" ] }

Select few Property instead of all:
        db.mycollection.find({skill:{$in:["c#","ccna"]}},{fname:1,mname:1,lname:1})
    output:
        { "_id" : ObjectId("5a4634cf26837e3337746881"), "fname" : "john", "mname" : "joe", "lname" : "doe" }
        { "_id" : ObjectId("5a4634cf26837e3337746882"), "fname" : "mary", "mname" : "joe", "lname" : "doe" }

No comments:

Post a Comment