Friday, January 2, 2015
MongoDB - Misc Stuff
-- All are done as root
-- Node1
-- Node2
-- Node3
-- Moongo softare /apps/mongo
-- Mongo data /data/mongo
-- Mongo logs /logs/mongo
=====================
Mongod servers Node1
=====================
mongod --logpath /logs/mongo/n1_mongod.log --logappend --fork --port 27017 --dbpath /data/mongo/n1 --shardsvr --rest --replSet rs_a --oplogSize 128
mongod --logpath /logs/mongo/n2_mongod.log --logappend --fork --port 27018 --dbpath /data/mongo/n2 --shardsvr --rest --replSet rs_a --oplogSize 128
mongod --logpath /logs/mongo/n3_mongod.log --logappend --fork --port 27019 --dbpath /data/mongo/n3 --shardsvr --rest --replSet rs_a --oplogSize 128
>rs.initiate()
>rs.add("node1":"27018")
>rs.add("node1":"27019")
>rs.status()
>rs.isMaster()
db.serverStatus().connections
=====================
config servers Node1
=====================
mongod --configsvr --dbpath /data/mongo/configdb/server1 --port 27020 --logpath /logs/mongo/config_server1.log --logappend &
mongod --configsvr --dbpath /data/mongo/configdb/server2 --port 27021 --logpath /logs/mongo/config_server2.log --logappend &
mongod --configsvr --dbpath /data/mongo/configdb/server3 --port 27022 --logpath /logs/mongo/config_server3.log --logappend &
[root@node1 config]# show
root 20333 0.0 0.4 1549344 39272 ? Sl May03 1:08 mongod -f /apps/mongo/n1_mongod.conf
root 20407 0.0 0.4 1538028 38568 ? Sl May03 0:58 mongod -f /apps/mongo/n2_mongod.conf
root 20435 0.0 0.4 1579048 39552 ? Sl May03 1:12 mongod -f /apps/mongo/n3_mongod.conf
[root@node1 config]# cat /apps/mongo/n1_mongod.conf
logpath=/logs/mongo/n1_mongod.log
logappend=true
fork = true
port = 27017
dbpath=/data/mongo/n1
#shardsvr=false
rest=true
replSet=rs_a
oplogSize=128
[root@node1 config]# cat /apps/mongo/n2_mongod.conf
logpath=/logs/mongo/n2_mongod.log
logappend=true
fork = true
port = 27018
dbpath=/data/mongo/n2
#shardsvr=false
rest=true
replSet=rs_a
oplogSize=128
[root@node1 config]# cat /apps/mongo/n3_mongod.conf
logpath=/logs/mongo/n3_mongod.log
logappend=true
fork = true
port = 27019
dbpath=/data/mongo/n3
#shardsvr=false
rest=true
replSet=rs_a
oplogSize=128
Node2:
===========
mongos --configdb <config server hostnames>
mongos --configdb node1:27020,node1:27021,node1:27022 --port 27025 --logpath /logs/mongo/mongos@port27020.log --logappend &
sh.addShard( "rs_a/node1:27017,node1:27018,node1:27019")
Install
=========
curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.4.3.tgz > /apps/mongo/mongodb.tgz
cd /apps/mongo/
tar -zxvf mongodb.tgz
[root@node1 mongo]# cat mongod.conf
logpath=/logs/mongo/mongod.log
logappend=true
fork = true
port = 27017
dbpath=/data/mongo
#shardsvr=false
rest=true
replSet=rs_a
oplogSize=128
export PATH=$PATH:$HOME/bin:/root/mongo/mongodb-linux-x86_64-2.4.3/bin
mongod --dbpath /data/mongo/ --port 27017 --logpath /logs/mongo/mongod.log &
--mongo --username <user> --password <pass> --hostname <host> --port 28015
--mongo -u <user> -p <pass> --host <host> --port 28015 --shell --norc /root/mongo/a.js
-- get output from linux prompt
To print return a query as JSON, from the system prompt using the --eval option, use the following form:
mongo --eval 'db.collection.find().forEach(printjson)'
--------------
node1
============
mongo
use admin
rsconfig = {
_id : "rs_a",
members : [
{ _id : 0, host : "node1:27017"}
]
}
rs.initiate(rsconfig)
rs.status()
rs.add("node2:27017")
rs.add("node3:27017")
------------
When Node1 has all three members of replica set a
====================================================
Login in Node1
mongo --port 27017
use admin
rsconfig = {
_id : "rs_a",
members : [
{ _id : 0, host : "node1:27017"}
]
}
rs.initiate(rsconfig)
rs.status()
rs_a:STARTUP2> rs.status() <=== Shows Just One Member
{
"set" : "rs_a",
"date" : ISODate("2013-05-03T16:09:42Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 517,
"optime" : {
"t" : 1367597379,
"i" : 1
},
"optimeDate" : ISODate("2013-05-03T16:09:39Z"),
"self" : true
}
],
"ok" : 1
}
rs.add("node1:27018")
rs.add("node1:27019")
rs.status()
------------
db.help()
db.<collection>.help()
db <- show the current working DB
db.stats()
db.stats(1024) <-- KB
db.auth()
db.collectionName.stats() <- Shows number of Rows, size of table, Indexes Etc
db.hostInfo()
db.currentOp() <-- Current Operation Reporting
db.adminCommand( { "hostInfo" : 1 } )
local.startup_log <--?
-- Config database info
use config
show collections
show dbs <-show all databases in the Running Mongo
show users
show profile
show databases
mongo machine/dbname <- conect any database using machine & db
mongoimport --help <= import from file
mongoimport --db <dbname> --collection <collectionmame> --port nnnn(masterPort) < a.json
db.isMaster()
top
-- Drop stuff
db.collection.drop();
db.dropDatabase();
Alternatively, you can stop mongod and delete the data files from your data directory, then restart.
db.collection.dropIndex("age_1");
mongo <dbname> --eval "db.dropDatabase()" <- From linux Shell
db.collection.insert( <document> ) <-- Insert
use person
db.bio.insert(
{
name: { first: 'John', last: 'Backus' },
birth: new Date('Dec 03, 1924'),
death: new Date('Mar 17, 2007'),
contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ],
awards: [
{award: 'W.W. McDowell Award', year: 1967,by: 'IEEE Computer Society'},
{award: 'National Science', year: 1975,by: 'National Science Foundation'},
{award: 'Turing Award', year: 1977,by: 'ACM'},
{award: 'Draper Prize', year: 1993,by: 'National Academy of Engineering'}
]
}
)
Java scripy need Quote
"$style"
=========================== File address.json ================================
{name:{fn: 'Adarsh20',last: 'Kumar20'},loc:'A',ph:'20',state:['MO','VA','PA']}
{name:{fn: 'Adarsh10',last: 'Kumar10'},loc:'C',ph:'10',state:['CO','VA','CA']}
{name:{fn: 'Adarsh11',last: 'Kumar11'},loc:'D',ph:'11',state:['MO','VA','PA']}
{name:{fn: 'Adarsh14',last: 'Kumar14'},loc:'B',ph:'14',state:['BO','TA','PA']}
{name:{fn: 'Adarsh16',last: 'Kumar16'},loc:'Z',ph:'16',state:['MO','VA','AK']}
{name:{fn: 'Adarsh15',last: 'Kumar14'},loc:'H',ph:'15',state:['MO','KA','PA']}
{name:{fn: 'Adarsh18',last: 'Kumar18'},loc:'I',ph:'18',state:['TO','VA','MA']}
{name:{fn: 'Adarsh19',last: 'Kumar19'},loc:'E',ph:'19',state:['MO','VA','AA']}
./mongoimport --db emp --collection address --port 27017 < address.json
db.address.find({loc:"A"}) <= list all where loc='A'
db.address.find({},{loc:0}) <==loc:1(show me only loc field), loc:0 is reverse
db.address.findOne({"_id":ObjectId("518954aa4b45b16e6fc564dc")})
==============================================================================
Sorting
db.address.find().sort({"ph":1}) <= 1 asc -1 desc
db.address.find().sort({fn:1,ln:-1) <= composite sort
use emp
db.address.insert(
{
name: { fn: 'Adarsh1',last: 'Kumar1' },
loc:'A',
ph:'111111',
state:['MO','VA','PA']
}
)
db.address.insert(
{
name: { fn: 'Adarsh2',last: 'Kumar2' },
loc:'B',
ph:'222222',
state:['MO','VA','PA']
}
)
db.address.insert(
{
name: { fn: 'Adarsh3',last: 'Kumar3' },
loc:'B',
ph:'333333',
state:['MO','VA','PA']
}
)
db.address.insert(
{
name: { fn: 'Adarsh4',last: 'Kumar4' },
loc:'A',
ph:'111111',
state:['MO','VA','PA']
}
)
Consider the following operation to insert two documents, using the _id field of the first document as a reference in the second document:
original_id = ObjectId()
db.places.insert({
"_id": original_id
"name": "Broadway Center"
"url": "bc.example.net"
})
db.people.insert({
"name": "Erin"
"places_id": original_id
"url": "bc.example.net/Erin"
})
db <-- display the current db
db.collection.findOne() <- return one doc
db.collection.find() <- Return All Documents in a Collection
db.collection.find().pretty()
db.collection.find( { name: { first: 'John', last: 'McCarthy' } } )
db.collection.find( {_id: 5} ) <- show all has id=5
db.collection.find({},{phone:1}) <= show all document with column name ="phone"
db.product.find({price:12.5}).toArray() <= list field from product which has price=12.5
db.product.find({},{price:1}) <= list field from product which has price element
db.collection.find().limit(10).toArray() <= convert list in an arry and show 10 Document
db.address.totalSize()
db.address.dataSize()
db.address.count()
db.collection.remove( <query>, <justOne> )
db.collection.remove( { 'name.first' : /^G/ } ) <-- del All subdocument name contains a field first whose value starts with G
db.collection.remove( { turing: true }, 1 ) <-- remove single matching Doc
db.collection.remove() <- remove all doc from collection
db.system.indexes.find() <- List all Indexes for a Database
db.collection.getIndexes() <- List all Indexes on a Collection
db.collection.ensureIndex( { a: 1, b: 1, c: 1 } ) <-- Build a Compound Index
db.collection.ensureIndex( { item: 1, category: 1, price: 1 } )
db.collection.dropIndex( { "tax-id": 1 } ) <-- Remove Index
-- Import Document
Goto Mongo Install
mongoimport --help <=== import json,csv,tsv
-- Upsert Mode
db.collection.update( <query>,
<update>,
{ upsert: true } )
•Count the number of all documents in the orders collection:
db.runCommand( { count: 'orders' } )
db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } }
} )
The clone command clone a database from a remote MongoDB instance to the current host.
clone copies the database on the remote instance with the same name as the current database.
The command takes the following form:
{ clone: "db1.example.net:27017" }
The cloneCollection command copies a collection from a remote server to the server where you run the command. cloneCollection does not allow you to clone a collection through a mongos: you must connect directly to the mongod instance.
{ cloneCollection: "users.profiles", from: "mongodb.example.net:27017", query: { active: true }, copyIndexes: false }
This document explains the output of the $explain operator and the mongo shell method explain().
getParameter is an administrative command for retrieving the value of options normally set on the command line. Issue commands against the admin database as follows:
{ getParameter: 1, <option>: 1 }
The values specified for getParameter and <option> do not affect the output. The command works with the following options:
•quiet
•notablescan
•logLevel
•syncdelay
The ping command is a no-op used to test whether a server is responding to commands. This command will return immediately even if the server is write-locked:
{ ping: 1 }
The shutdown command cleans up all database resources and then terminates the process. You must issue the shutdown command against the admin database in the form:
{ shutdown: 1 }
Note:
Run the shutdown against the admin database. When using shutdown, the connection must originate from localhost or use an authenticated connection.
If the node you’re trying to shut down is a replica set primary, then the command will succeed only if there exists a secondary node whose oplog data is within 10 seconds of the primary. You can override this protection using the force option:
{ shutdown: 1, force: true }
Alternatively, the shutdown command also supports a timeoutSecs argument which allows you to specify a number of seconds to wait for other members of the replica set to catch up:
{ shutdown: 1, timeoutSecs: 60 }
The equivalent mongo shell helper syntax looks like this:
db.shutdownServer({timeoutSecs: 60});
-- Prompt
Example:
Create a prompt with the number of commands issued in the current session, define the following variables:
cmdCount = 1;
prompt = function() {
return (cmdCount++) + "> ";
}
To create a mongo shell prompt in the form of <database>@<hostnane>$ define the following variables:
host = db.serverStatus().host;
prompt = function(){return host+"@"+db+"> ";}
== Comand
rs_a:PRIMARY> rs.isMaster()
{
"setName" : "rs_a",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"node1:27017",
"node3:27017",
"node2:27017"
],
"primary" : "node1:27017",
"me" : "node1:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-05-03T14:03:28.961Z"),
"ok" : 1
}
How to make Slave read ok
---------------------------
rs_a:SECONDARY> rs.slaveOk()
rs_a:SECONDARY> db.getMongo().slaveOk
Ans : true
How to remove a node from replica set
=====================================
-connect mongo with appropriate port on any master
-connect to mongo shell [mongo]
>use admin
>rs.isMaster() <- confirm all nodes & port listed in the results
>rs.remove("nodeName:portnumber) example > rs.remove("node1:27017")
>rs.isMaster() <- confirm that node is removed
rs_a:SECONDARY> db.printReplicationInfo()
configured oplog size: 128MB
log length start to end: 233secs (0.06hrs)
oplog first event time: Fri May 03 2013 12:09:39 GMT-0400 (EDT)
oplog last event time: Fri May 03 2013 12:13:32 GMT-0400 (EDT)
now: Fri May 03 2013 12:17:49 GMT-0400 (EDT)
How to list System stats mongoversions etc uptime pid
======================================================
rs_a:SECONDARY> db.serverStatus()
{
"host" : "node1.carfax.cfx",
"version" : "2.4.3",
"process" : "mongod",
"pid" : 20333,
"uptime" : 1066,
"uptimeMillis" : NumberLong(1066444),
"uptimeEstimate" : 758,
"localTime" : ISODate("2013-05-03T16:18:51.989Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 1,
"rollovers" : 0
},
"backgroundFlushing" : {
"flushes" : 17,
"total_ms" : 30313,
"average_ms" : 1783.1176470588234,
"last_ms" : 0,
"last_finished" : ISODate("2013-05-03T16:18:05.706Z")
},
"connections" : {
"current" : 5,
"available" : 814,
"totalCreated" : NumberLong(43)
},
"cursors" : {
"totalOpen" : 4,
"clientCursors_size" : 4,
"timedOut" : 0
},
"dur" : {
"commits" : 28,
"journaledMB" : 0,
"writeToDataFilesMB" : 0,
"compression" : 0,
"commitsInWriteLock" : 0,
"earlyCommits" : 0,
"timeMs" : {
"dt" : 3039,
"prepLogBuffer" : 0,
"writeToJournal" : 0,
"writeToDataFiles" : 0,
"remapPrivateView" : 0
}
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 62751064,
"page_faults" : 74
},
"globalLock" : {
"totalTime" : NumberLong(1066445000),
"lockTime" : NumberLong(12805922),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 0,
"readers" : 0,
"writers" : 0
}
},
"indexCounters" : {
"accesses" : 4,
"hits" : 4,
"misses" : 0,
"resets" : 0,
"missRatio" : 0
},
"locks" : {
"." : {
"timeLockedMicros" : {
"R" : NumberLong(40263),
"W" : NumberLong(12805922)
},
"timeAcquiringMicros" : {
"R" : NumberLong(15375030),
"W" : NumberLong(4037)
}
},
"admin" : {
"timeLockedMicros" : {
},
"timeAcquiringMicros" : {
}
},
"local" : {
"timeLockedMicros" : {
"r" : NumberLong(59452),
"w" : NumberLong(118585)
},
"timeAcquiringMicros" : {
"r" : NumberLong(2836175),
"w" : NumberLong(224)
}
},
"person" : {
"timeLockedMicros" : {
"r" : NumberLong(18908),
"w" : NumberLong(2903934)
},
"timeAcquiringMicros" : {
"r" : NumberLong(41),
"w" : NumberLong(26)
}
}
},
"network" : {
"bytesIn" : 72696,
"bytesOut" : 99101,
"numRequests" : 688
},
"opcounters" : {
"insert" : 2,
"query" : 85,
"update" : 8,
"delete" : 0,
"getmore" : 121,
"command" : 548
},
"opcountersRepl" : {
"insert" : 0,
"query" : 0,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 0
},
"recordStats" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0,
"local" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0
},
"person" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0
}
},
"repl" : {
"setName" : "rs_a",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"node1:27017",
"node1:27019",
"node1:27018"
],
"primary" : "node1:27019",
"me" : "node1:27017"
},
"writeBacksQueued" : false,
"mem" : {
"bits" : 64,
"resident" : 36,
"virtual" : 1513,
"supported" : true,
"mapped" : 416,
"mappedWithJournal" : 832
},
"metrics" : {
"document" : {
"deleted" : NumberLong(0),
"inserted" : NumberLong(2),
"returned" : NumberLong(30),
"updated" : NumberLong(8)
},
"getLastError" : {
"wtime" : {
"num" : 1,
"totalMillis" : 0
},
"wtimeouts" : NumberLong(0)
},
"operation" : {
"fastmod" : NumberLong(6),
"idhack" : NumberLong(0),
"scanAndOrder" : NumberLong(0)
},
"queryExecutor" : {
"scanned" : NumberLong(52)
},
"record" : {
"moves" : NumberLong(2)
},
"repl" : {
"apply" : {
"batches" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0)
},
"buffer" : {
"count" : NumberLong(0),
"maxSizeBytes" : 268435456,
"sizeBytes" : NumberLong(0)
},
"network" : {
"bytes" : NumberLong(684),
"getmores" : {
"num" : 19,
"totalMillis" : 96016
},
"ops" : NumberLong(0),
"readersCreated" : NumberLong(16)
},
"oplog" : {
"insert" : {
"num" : 4,
"totalMillis" : 0
},
"insertBytes" : NumberLong(855)
},
"preload" : {
"docs" : {
"num" : 0,
"totalMillis" : 0
},
"indexes" : {
"num" : 0,
"totalMillis" : 0
}
}
},
"ttl" : {
"deletedDocuments" : NumberLong(0),
"passes" : NumberLong(15)
}
},
"ok" : 1
rs_a:PRIMARY> db.serverStatus().mem
{
"bits" : 64,
"resident" : 35,
"virtual" : 1534,
"supported" : true,
"mapped" : 416,
"mappedWithJournal" : 832
}
rs_a:PRIMARY> db.serverStatus().network
{ "bytesIn" : 227110, "bytesOut" : 656548, "numRequests" : 2633 }
============ JSON Java Script object notation =======
JSON
{
x:3,
y:"hello",
z:[1,2],
nest:{...} eq nest:{q:99}
}
RDBMS
Table A
--------------------
X | Y | _id |
--------------------
3 | hello | 123 |
--------------------
Table B
---------
id | Z |
---------
123 | 1 |
123 | 2 |
---------
Note Table A and Table B are conned Via FK
https://www.youtube.com/watch?feature=player_embedded&v=CEH0kNxVyMw
https://www.youtube.com/watch?feature=player_embedded&v=W4o8HJyL7_8
- java scripts
https://www.youtube.com/watch?feature=player_embedded&v=n1Hld15pvZY
https://www.youtube.com/watch?feature=player_embedded&v=n79mmmQxoNE
-- import
https://www.youtube.com/watch?feature=player_embedded&v=ZWnHhKiApVU
-- Mongo Shell Queries
https://www.youtube.com/watch?feature=player_embedded&v=P1yLtecuLM8
-- Sorting
https://www.youtube.com/watch?feature=player_embedded&v=Ezx1lXaY7jE
-- Cursors
https://www.youtube.com/watch?feature=player_embedded&v=DRs4_hBpPlI