Sunday 28 August 2016

Replication in mongoDB step by step.



                                               Replication in mongoDB step by step.

Operating system details:-

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

mongoDB version details:-

MongoDB shell version: 2.6.12

Node details(servers)

node1: mongodb1.example.com

node2: mongodb2.example.com

node3: mongodb3.example.com

note:- i configured DNS server

1) create directory for dbpath (location of files)

Node1:

-bash-3.2$ pwd
/var/lib/mongo

-bash-3.2$ mkdir 1

-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:29 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:29 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:04 1

Node2:

-bash-3.2$ pwd
/var/lib/mongo
-bash-3.2$ mkdir 2
-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:29 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:29 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:05 2

Node3:

-bash-3.2$ mkdir 3
-bash-3.2$ pwd
/var/lib/mongo
-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:28 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:28 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:06 3
-bash-3.2$

2)  Create replication set and start mongod process in each server

Node1:

-bash-3.2$ mongod --port 27001 --replSet abc --dbpath /var/lib/mongo/1 --logpath /var/lib/mongo/log.1 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4146
child process started successfully, parent exiting

Node2:

-bash-3.2$ mongod --port 27002 --replSet abc --dbpath /var/lib/mongo/2 --logpath /var/lib/mongo/log.2 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 8573
child process started successfully, parent exiting

Node3:

-bash-3.2$ mongod --port 27003 --replSet abc --dbpath /var/lib/mongo/3 --logpath /var/lib/mongo/log.3 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 8499
child process started successfully, parent exiting


3) Connect to mongoDB on primary Server for initiat replication set

-bash-3.2$ mongo --port 27001
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27001/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
                               
> rs.initiate()                    -------initiat replication set
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "mongodb1.example.com:27001",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.status()
{
        "set" : "abc",
        "date" : ISODate("2016-08-28T22:44:30Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb1.example.com:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 326,
                        "optime" : Timestamp(1472424262, 1),
                        "optimeDate" : ISODate("2016-08-28T22:44:22Z"),
                        "electionTime" : Timestamp(1472424262, 2),
                        "electionDate" : ISODate("2016-08-28T22:44:22Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
abc:PRIMARY>
abc:PRIMARY>

4)  Add secondary servers to replication set.

abc:PRIMARY> rs.add("mongodb2.example.com:27002")
{ "ok" : 1 }
abc:PRIMARY> rs.add("mongodb3.example.com:27003"
... )
{ "ok" : 1 }

abc:PRIMARY> rs.initiate()
{
        "info" : "try querying local.system.replset to see current configuration",
        "ok" : 0,
        "errmsg" : "already initialized"
}
abc:PRIMARY> rs.status()
{
        "set" : "abc",
        "date" : ISODate("2016-08-28T22:47:58Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb1.example.com:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 534,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "electionTime" : Timestamp(1472424262, 2),
                        "electionDate" : ISODate("2016-08-28T22:44:22Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "mongodb2.example.com:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 44,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "lastHeartbeat" : ISODate("2016-08-28T22:47:57Z"),
                        "lastHeartbeatRecv" : ISODate("2016-08-28T22:47:57Z"),
                        "pingMs" : 14,
                        "syncingTo" : "mongodb1.example.com:27001"
                },
                {
                        "_id" : 2,
                        "name" : "mongodb3.example.com:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 35,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "lastHeartbeat" : ISODate("2016-08-28T22:47:57Z"),
                        "lastHeartbeatRecv" : ISODate("2016-08-28T22:47:57Z"),
                        "pingMs" : 48,
                        "syncingTo" : "mongodb1.example.com:27001"
                }
        ],
        "ok" : 1
}
abc:PRIMARY>

5)  connect to secondary server

Node2:

-bash-3.2$ mongo --port 27002
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27002/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
abc:SECONDARY>

Node3:

-bash-3.2$ mongo --port 27003
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27003/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
abc:SECONDARY>

use rs.slaveOk() command for apply the replication on secodary servers
example:
abc:SECONDARY>rs.slaveOk()

6) Test replication is working

abc:PRIMARY> show dbs
admin  (empty)
local  0.094GB
abc:PRIMARY> db.products.insert({ "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 })
WriteResult({ "nInserted" : 1 })
abc:PRIMARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }

Node2:

abc:SECONDARY> show dbs
admin  (empty)
local  0.094GB
abc:SECONDARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }
abc:SECONDARY>

Node3:

abc:SECONDARY> show dbs
admin  (empty)
local  0.094GB
abc:SECONDARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }



 Reference :-http://www.tutorialspoint.com

Saturday 13 August 2016

CRUD operations in MongoDB


create,read,update and delete operation in mongoDB

I have below collections in my db, I will perform all operation on products collections.

> show collections
products
products_bak
system.indexes
>

1) insert records in collections.

> db.products.find().count()
12

> db.products.insert({ "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 })
WriteResult({ "nInserted" : 1 })

> db.products.find().count()
13

>  db.products.find()

{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9"), "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa"), "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb"), "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc"), "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd"), "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } }, "sales_tax" : true, "term_years" : 3 }
{ "_id" : ObjectId("507d95d5719dbef170f15c01"), "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }
{ "_id" : ObjectId("57af8659f78428671f128162"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }


note:- mongoDB collection usually created implicitly.


2) read data from collections.

- db.collection.find() command use for read data from collections
> db.products.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9"), "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa"), "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb"), "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc"), "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd"), "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } }, "sales_tax" : true, "term_years" : 3 }
{ "_id" : ObjectId("507d95d5719dbef170f15c01"), "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }
{ "_id" : ObjectId("57af87dbf78428671f128163"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 5000 }

- lets check count of document in collection

> db.products.find().count()
12                    ----there are 12 documment in collection

- find perticuler document from collection..

> db.products.find({},{"limits"=1})
2016-08-14T01:49:00.937+0530 SyntaxError: Unexpected token =
> db.products.find({},{"limits":1})
{ "_id" : "ac3" }
{ "_id" : "ac7" }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9") }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa") }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb") }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc") }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd") }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } } }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } } }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } } }
{ "_id" : ObjectId("507d95d5719dbef170f15c01") }
{ "_id" : "ac9" }

note:- all document where name exists limits..

- only deplay records where limits have the values

>db.products.find({limits:{$exists:true}},{"limits" :1})

{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } } }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } } }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } } }

-deplay the records without _id

> db.products.find({limits:{$exists:true}},{"limits" :1 , _id:0})

{ "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } } }
{ "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } } }
{ "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } } }

3) update the recods in collections.

> db.products.update( {"emp_name" : "shiv"},{$set:{"salary" : 5000}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.products.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9"), "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa"), "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb"), "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc"), "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd"), "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } }, "sales_tax" : true, "term_years" : 3 }
{ "_id" : ObjectId("507d95d5719dbef170f15c01"), "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }
{ "_id" : ObjectId("57af87dbf78428671f128163"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 5000 }

4) remove document(record) from collection.

> db.products.remove({"salary" : 5000 })
WriteResult({ "nRemoved" : 1 })

> db.products.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9"), "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa"), "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb"), "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc"), "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd"), "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe"), "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : ObjectId("507d95d5719dbef170f15bff"), "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : ObjectId("507d95d5719dbef170f15c00"), "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } }, "sales_tax" : true, "term_years" : 3 }
{ "_id" : ObjectId("507d95d5719dbef170f15c01"), "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }


reference:-https://docs.mongodb.com/manual/reference/command/ 

Saturday 30 July 2016

how to install Mongo DB on linux..

->how to install Mongo DB on linux?

OS details:

[root@localhost ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
[root@localhost ~]#

mongo db version Details.

mongodb-org-3.0

installation steps

1) check network connections

[root@MongoDB ~]# ping mongodb.com
PING mongodb.com (52.21.89.200) 56(84) bytes of data.
64 bytes from ec2-52-21-89-200.compute-1.amazonaws.com (52.21.89.200): icmp_seq=1 ttl=45 time=224 ms
64 bytes from ec2-52-21-89-200.compute-1.amazonaws.com (52.21.89.200): icmp_seq=2 ttl=45 time=227 ms

--- mongodb.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2001ms
rtt min/avg/max/mdev = 224.843/226.084/227.325/1.241 ms

2) create and add below paramters in vi /etc/yum.repos.d/mongodb-org-3.0.repo file 

[root@MongoDB ~]# cat /etc/yum.repos.d/mongodb-org-3.0.repo
[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1

3) Run yum command to install mongoDB RPM

[root@MongoDB ~]# yum install -y mongodb-org
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:3.0.12-1.el5 set to be updated
--> Processing Dependency: mongodb-org-tools = 3.0.12 for package: mongodb-org
--> Processing Dependency: mongodb-org-shell = 3.0.12 for package: mongodb-org
--> Processing Dependency: mongodb-org-server = 3.0.12 for package: mongodb-org
--> Processing Dependency: mongodb-org-mongos = 3.0.12 for package: mongodb-org
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:3.0.12-1.el5 set to be updated
---> Package mongodb-org-server.x86_64 0:3.0.12-1.el5 set to be updated
---> Package mongodb-org-shell.x86_64 0:3.0.12-1.el5 set to be updated
---> Package mongodb-org-tools.x86_64 0:3.0.12-1.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================
 Package                                Arch                       Version                           Repository                           Size
===============================================================================================================================================
Installing:
 mongodb-org                            x86_64                     3.0.12-1.el5                      mongodb-org-3.0                     5.7 k
Installing for dependencies:
 mongodb-org-mongos                     x86_64                     3.0.12-1.el5                      mongodb-org-3.0                     4.7 M
 mongodb-org-server                     x86_64                     3.0.12-1.el5                      mongodb-org-3.0                     9.8 M
 mongodb-org-shell                      x86_64                     3.0.12-1.el5                      mongodb-org-3.0                     5.0 M
 mongodb-org-tools                      x86_64                     3.0.12-1.el5                      mongodb-org-3.0                      28 M

Transaction Summary
===============================================================================================================================================
Install      5 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total size: 48 M
Total download size: 28 M
Downloading Packages:
mongodb-org-tools-3.0.12-1.el5.x86_64.rpm                                                                               |  28 MB     03:29
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : mongodb-org-server                                                                                                      1/5
  Installing     : mongodb-org-shell                                                                                                       2/5
  Installing     : mongodb-org-tools                                                                                                       3/5
  Installing     : mongodb-org-mongos                                                                                                      4/5
  Installing     : mongodb-org                                                                                                             5/5

Installed:
  mongodb-org.x86_64 0:3.0.12-1.el5

Dependency Installed:
  mongodb-org-mongos.x86_64 0:3.0.12-1.el5       mongodb-org-server.x86_64 0:3.0.12-1.el5       mongodb-org-shell.x86_64 0:3.0.12-1.el5
  mongodb-org-tools.x86_64 0:3.0.12-1.el5

Complete!
[root@MongoDB ~]#

4) MongoDB details 

[root@MongoDB ~]# service mongod status
mongod is stopped
[root@MongoDB ~]# service mongod start
Starting mongod:                                           [  OK  ]
[root@MongoDB ~]# service mongod status
mongod (pid 3672) is running...
[root@MongoDB ~]#

=> mongoDB logs 

[root@MongoDB ~]# cat /var/log/mongodb/mongod.log
2016-07-30T17:55:37.721+0530 I CONTROL  ***** SERVER RESTARTED *****
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] MongoDB starting : pid=3672 port=27017 dbpath=/var/lib/mongo 64-bit host=MongoDB.example.com
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] db version v3.0.12
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] git version: 33934938e0e95d534cebbaff656cde916b9c3573
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] build info: Linux build13.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2016-07-30T17:55:37.725+0530 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-07-30T17:55:37.751+0530 I JOURNAL  [initandlisten] journal dir=/var/lib/mongo/journal
2016-07-30T17:55:37.751+0530 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2016-07-30T17:55:37.898+0530 I JOURNAL  [durability] Durability thread started
2016-07-30T17:55:37.898+0530 I JOURNAL  [journal writer] Journal writer thread started
2016-07-30T17:55:37.901+0530 I INDEX    [initandlisten] allocating new ns file /var/lib/mongo/local.ns, filling with zeroes...
2016-07-30T17:55:37.944+0530 I STORAGE  [FileAllocator] allocating new datafile /var/lib/mongo/local.0, filling with zeroes...
2016-07-30T17:55:37.944+0530 I STORAGE  [FileAllocator] creating directory /var/lib/mongo/_tmp
2016-07-30T17:55:38.286+0530 I STORAGE  [FileAllocator] done allocating datafile /var/lib/mongo/local.0, size: 64MB,  took 0.298 secs
2016-07-30T17:55:38.292+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2016-07-30T17:58:05.287+0530 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2016-07-30T17:58:05.287+0530 I CONTROL  [signalProcessingThread] now exiting
2016-07-30T17:58:05.287+0530 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2016-07-30T17:58:05.287+0530 I NETWORK  [signalProcessingThread] closing listening socket: 6
2016-07-30T17:58:05.287+0530 I NETWORK  [signalProcessingThread] closing listening socket: 7
2016-07-30T17:58:05.288+0530 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2016-07-30T17:58:05.288+0530 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2016-07-30T17:58:05.288+0530 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2016-07-30T17:58:05.288+0530 I STORAGE  [signalProcessingThread] shutdown: waiting for fs preallocator...
2016-07-30T17:58:05.288+0530 I STORAGE  [signalProcessingThread] shutdown: final commit...
2016-07-30T17:58:05.288+0530 I JOURNAL  [signalProcessingThread] journalCleanup...
2016-07-30T17:58:05.288+0530 I JOURNAL  [signalProcessingThread] removeJournalFiles
2016-07-30T17:58:05.311+0530 I JOURNAL  [signalProcessingThread] Terminating durability thread ...
2016-07-30T17:58:05.394+0530 I JOURNAL  [journal writer] Journal writer thread stopped
2016-07-30T17:58:05.394+0530 I JOURNAL  [durability] Durability thread stopped
2016-07-30T17:58:05.394+0530 I STORAGE  [signalProcessingThread] shutdown: closing all files...
2016-07-30T17:58:05.395+0530 I STORAGE  [signalProcessingThread] closeAllFiles() finished
2016-07-30T17:58:05.395+0530 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2016-07-30T17:58:05.395+0530 I CONTROL  [signalProcessingThread] dbexit:  rc: 0


[root@MongoDB ~]# mongod --version
db version v3.0.12
git version: 33934938e0e95d534cebbaff656cde916b9c3573

[root@MongoDB ~]# mongo -version
MongoDB shell version: 3.0.12


:- Uninstall MongoDB

[root@MongoDB ~]# service mongod stop
Stopping mongod:                                           [  OK  ]
[root@MongoDB ~]# service mongod status
mongod is stopped
[root@MongoDB ~]#

[root@MongoDB ~]# yum erase $(rpm -qa | grep mongodb-org)

Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:3.0.12-1.el5 set to be erased
---> Package mongodb-org-mongos.x86_64 0:3.0.12-1.el5 set to be erased
---> Package mongodb-org-server.x86_64 0:3.0.12-1.el5 set to be erased
---> Package mongodb-org-shell.x86_64 0:3.0.12-1.el5 set to be erased
---> Package mongodb-org-tools.x86_64 0:3.0.12-1.el5 set to be erased
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================
 Package                                 Arch                        Version                              Repository                      Size
===============================================================================================================================================
Removing:
 mongodb-org                             x86_64                      3.0.12-1.el5                         installed                       0.0
 mongodb-org-mongos                      x86_64                      3.0.12-1.el5                         installed                       13 M
 mongodb-org-server                      x86_64                      3.0.12-1.el5                         installed                       26 M
 mongodb-org-shell                       x86_64                      3.0.12-1.el5                         installed                       14 M
 mongodb-org-tools                       x86_64                      3.0.12-1.el5                         installed                       92 M

Transaction Summary
===============================================================================================================================================
Install      0 Package(s)
Update       0 Package(s)
Remove       5 Package(s)

Is this ok [y/N]:y

[root@MongoDB ~]# rm -r /var/log/mongodb
[root@MongoDB ~]# rm -r /var/lib/mongo