Sunday, November 25, 2018


MongoDB Operators

Fields Description

$currentDate à   Sets the value of a field to current date, either as a Date or a Timestamp.
$inc à  Increments the value of the field by the specified amount.
$min à   Only updates the field if the specified value is less than the existing field value.
$max à Only updates the field if the specified value is greater than the existing field value.
$mul à Multiplies the value of the field by the specified amount.
$rename à Renames a field.
$set à Sets the value of a field in a document.
$setOnInsert à Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$unset à Removes the specified field from a document.

Array Operators

$    Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSet à  Adds elements to an array only if they do not already exist in the set.
$pop à Removes the first or last item of an array.
$pull à removes all array elements that match a specified query.
$pushAll à   Deprecated. Adds several items to an array.
$push à Adds an item to an array.
$pullAll à      Removes all matching values from an array.

Modifiers

$each à Modifies the $push and $addToSet operators to append multiple items for array updates.
$position à  Modifies the $push operator to specify the position in the array to add elements.
$slice à Modifies the $push operator to limit the size of updated arrays.
$sort à Modifies the $push operator to reorder documents stored in an array.

Partial Update

$set Parameter
> db.sai.find()
{ "_id" : ObjectId("59f034e02559518339882775"), "a" :
{ "_id" : ObjectId("59f035092559518339882776"), "a" :
{ "_id" : 2, "a" : 42, "b" : "said", "dafa" : 232, "s

a=db.sai
a.insert({_id:1},{"name":"sai"})

$inc
> db.sai.insert({_id:3},{inc_sai:100})
WriteResult({ "nInserted" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3 }
> db.sai.insert({_id:3},{$inc{inc_sai:100})

> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 101 }
> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 102 }
> db.sai.update({_id:3},{$inc:{inc_sai:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 107 }

$push It will allows the duplicate values         

> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi" ] }
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi", "hi", "Ihi", "hi", "hi" ] }

$addToSet It prevents duplicate items from being added to the array

> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi", "hi", "hi", "hi", "hi", "bye" ] }



Saturday, November 17, 2018

MongoDB Replcation Setup

MongoDB Replica-Set Configuration
=========================================
we are configuring the Mongo replicaset on Linux server only for practice purpose

Primary+Seconday+Secondary

[mongodb@sainath data]$ pwd
/u02/mongo/data

Creating Directory's for the data path

[mongodb@sainath data]$ mkdir sai_r1
[mongodb@sainath data]$ mkdir sai_r2
[mongodb@sainath data]$ mkdir sai_r3

[mongodb@sainath data]$ ls -ltr

drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r1
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r2
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r3



ReplicaSet Name mentioned here is R

Port Numbers We have used are 27027 , 27028 , 27029

Startng Mongod service with the  below commands in different sessions
========================================================================
mongod --port 27027 --dbpath /u02/mongo/data/sai_r1 --replSet R
mongod --port 27028 --dbpath /u02/mongo/data/sai_r2 --replSet R
mongod --port 27029 --dbpath /u02/mongo/data/sai_r3 --replSet R

Connecting the mongo session

[mongodb@sainath data]$ mongo --port 27027
 MongoDB shell version v3.6.0-rc0
connecting to: mongodb://127.0.0.1:27027/
MongoDB server version: 3.6.0-rc0
Server has startup warnings:
2018-11-16T22:24:28.495+0530 I CONTROL  [initandlisten]

Initiate the replSet by using command rs.initiate()

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "sainath.com:27027",
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set R maps to this node",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}


Configurinng adding all the mongod servers to cluster replSet R
===============================================================
> config = { "_id": "R",
... "members" : [
... { "_id" : 0, "host" : "localhost:27027" },
... { "_id" : 1, "host" : "localhost:27028" },
... { "_id" : 2, "host" : "localhost:27029" }
... ]}
{
        "_id" : "R",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029"
                }
        ]
}
> rs.initiate(config)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387435, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387435, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>   rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T16:57:37.161Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 190,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1542387446, 1),
                        "electionDate" : ISODate("2018-11-16T16:57:26Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.115Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.526Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.116Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.528Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542387448, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387448, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY> rs.config()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5beef6eb7e1ec6c6b47764a4")
        }
}



Setting Priorities
======================
cfg=rs.conf()
cfg.members[0].priority=3
cfg.members[1].priority=4
cfg.members[2].priority=3
rs.reconfig(cfg)


R:PRIMARY> cfg=rs.conf()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5beef6eb7e1ec6c6b47764a4")
        }
}
R:PRIMARY> cfg.members[0].priority=3
3
R:PRIMARY> cfg.members[1].priority=4
4
R:PRIMARY> cfg.members[2].priority=3
3
R:PRIMARY> rs.reconfig(cfg)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387990, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387990, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>
> rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T17:11:06.924Z"),
        "myState" : 1,
        "term" : NumberLong(3),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 999,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "electionTime" : Timestamp(1542388260, 1),
                        "electionDate" : ISODate("2018-11-16T17:11:00Z"),
                        "configVersion" : 4,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:10:49Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:10:49Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:06.347Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:11:01Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:05.381Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 4
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542388261, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542388261, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Friday, November 16, 2018

MongoDB Export and Import Backup recoveries

Export Backups

mongoexport is a utility that produces a JSON or CSV export of data stored in a MongoDB instance.

Recommandation to Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

namespace options:
  -d, --db=<database-name>                        database to use
  -c, --collection=<collection-name>              collection to use
  -o, --out=<filename>                            output file; if not specified, stdout is used
  -f, --fields=<field>[,<field>]*                 comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
      --fieldFile=<filename>                      file with field names - 1 per line
  -q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'

Example for export backup of collection employyee
DB Name : sai
collection Name:  employee

R:PRIMARY> use sai
switched to db sai
R:PRIMARY> show collections
emplyee
R:PRIMARY> db.employee.count()
991
R:PRIMARY>

mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.json --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.json --port 27027

[mongodb@sainath backup]$ mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.json --port 27027
2018-11-17T00:00:27.863+0530    connected to: localhost:27027
2018-11-17T00:00:27.936+0530    exported 1982 records
[mongodb@sainath backup]$ mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee.json --port 27027
2018-11-17T00:01:13.742+0530    connected to: localhost:27027
2018-11-17T00:01:13.789+0530    exported 1982 records
[mongodb@sainath backup]$


[mongodb@sainath backup]$ ls -lrt
total 360
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
[mongodb@sainath backup]$

mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.Bson --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.Bson --port 27027

[mongodb@sainath backup]$ ls -rtl
total 720
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee.Bson
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee_1.Bson
[mongodb@sainath backup]$




mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.csv --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.csv --port 27027


mongoimport -d sai -c employee --type CSV --file /home/mongodb/Desktop/sai.csv --headerline --port 27027

mongoimport -d sai -c employee --type CSV --file /home/mongodb/Desktop/sai.csv --headerline --port 27027

[mongodb@sainath backup]$ ls -lr
total 1080
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:05 sai_employee.csv
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee.Bson
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:05 sai_employee_1.csv
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee_1.Bson
[mongodb@sainath backup]$