Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Thursday, February 5, 2015

MongoDB - Replication -Part I

I will be running Mongod instance in windows environment. I am planning to create one primary and 2 secondary instances.
First, I have created 3 folders in C:\
c:\MongoRepl\1
c:\MongoRepl\2
c:\MongoRepl\3

I will be using port no 27007 (primary) ,27008 ,27009. I have made sure these ports are allowed incoming/outgoing in firewall.

Now, I will create a standalone mongod instance first. Then, create a database on the instance.

mongod --dbpath c:\MongoRepl\1 --port 27007 --smallfiles --oplogSize 50



open another terminal and connect to Mongod shell and import replication database

mongo --port 27007 --shell replication.js



Now, Cloed the mongod shell. Restarted Mongod instance as a replica.

mongod --replSet Mya --dbpath c:\MongoRepl\1 --port 27007 --smallfiles --oplogSize 50



You will set EMPTYConfig error.


Next, we will start two other mongod instances (secondary), we will join them to replicaset Mya

mongod --replSet Mya --dbpath c:\MongoRepl\2 --port 27008 --smallfiles --oplogSize 50

mongod --replSet Mya --dbpath c:\MongoRepl\3 --port 27009 --smallfiles --oplogSize 50

Now, we will initiate a replica from mongo us initiate command.

rs.initiate()
rs.status()

create a configuration

cfg={
_id:"Mya",
members:[
{_id:0,host:"machinename:27007"},
{_id:1,host:"machinename:27008"},
{_id:2,host:"machinename:27009"},
]
}



Initiate Replica

rs.initiate(cfg)

Check replica status on primary 

rs.status()




Open termainal and start mongo

mongo --port 27008
mongo --port 27009




Read from secondary

I need to run rs.slaveOk() to read from secondary

rs.slaveOk()




Write to Primary

for(var i=0; i< 50000;i++){db.foo.insert({_id:i});sleep(i);}

While for statement is running, check on secondary rowcount

db.foo.count()




FailOver to Secondary

Run the command in Primary

rs.stepDown()

Now new primary is 27008



Now we will close Mongod instance on 27007.

rs.status()
27007 is unreachable since we shut down 27007



Reconfigure

On new primary replica. run config and rs.reconfig(cfg)

cfg={
_id:"Mya",
members:[
{_id:1,host:"machinename:27008"},
{_id:2,host:"machinename:27009"},
]
}

rs.reconfig(cfg)



Now, we have even number node.

Replica set information are stored in local db

use local
db.system.replset.find().pretty()

Wednesday, January 7, 2015

MongoDB - Dynamic_Schema/MongoImport/Cursor/Queries/Sorting

Dynamic Schema
-Polymorphism



Schemaless / Polymorphism

--add new fields easily to schema

Things
{Shape:"Rect",x:3,y:4,Area:12}
{Shape:"Circle",radius:1,Area:3.14}


Mongo Import

Open another command prompt

mongoimport.exe can import 3 types of files CSV,TSV,JSON
mongoimport --db databasename --collection tablename < filename

mongoimport --stopOnError
mongoimport --db pcat --collection products < products.json



Mongo Shell Queries


--show recordcount
db.collection.count()
--show all rows
db.collection.find()
--Show only name column
db.collection.find({},{name:1})
--show all rows nicely
db.collection.find().toArray()
--show only 10 rows nicely 
db.collection.find({}).limit(10).toArray()
--Show only 1 row
db.collection.findOne()
db.collection.find({}).limit(1)
--Skip first 2 and give next 4
db.collection.find({}).limit(4).Skip(2)   -
--Turn off Id
db.collection.find({},{name:1,_id:0})
--Get Name and Brand
db.collection.find({},{name:1,brand:1})
--All fields and 1 row
db.collection.find({}).limit(1)
--if the output is not too big
db.collection.find({}).limit(3).toArray()
--query price = 12.5
db.collection.find({price:12.5})
db.collection.find({price:12.5}).toArray()
db.collection.find({},{price:1})


--Object id is bson type, not string
-- Get one document
db.collection.findOne({"_id": ObjectId("ddadfa5464565fgafaddadffadfadfgffd")})




db.collection.find({price:200},{name:1,price:1})

--greater than
db.collection.find({price: {$gte:200}},{name:1,price:1})

$Operators

Querying

$gte
$gt
$lt
$lte
$or
$not
$nin
$in
$type
$exists

Updating

$inc
$set
$addToSet



--greater than and expression
db.collection.find({price: {$gte:200},available:true},{name:1,price:1,available:1})

--find ac3 is in array

db.collection.find({for : "ac3"}) 


Reaching into Nested Documents

dot notation

find({"x.a":1})

Quiz

Add caption
db.collection.find({for:"ac9",Type:"case"}).toArray()

Sorting

db.collection.find(_).sort(fieldname:direction)

db.collection.find({},{name:1,price:1}).sort({price:1})

-- null is less than number

--sort rows only with price

db.collection.find({price:{$exists:true}},{name:1,price:1}).sort({price:1})

db.collection.find({price:{$exists:true}},{name:1,price:1}).sort({price:-1})

Multiple Sort keys


db.collection.find().sort({lastname:1,firstname:1})

--SQL order by lastname,firstname

Cursors (different from other database)

db.test.find()
type it for more

db.test.find().skip(20).limit(5)

--skip is expensive, 20 is fine if 5000 is expensive.
--sort and skip is done at the server

Example

var cursor = db.test.find()
cursor.hasNext()

MongoDB - JSON/BSON/Documents

MongoDB - Lesson 1 

These are my notes from MongoDB university free DBA class.

MongoDB works on concept of collection and document.

MySQL   |   MongoDB  
Database = Database
Table = Collection
Row = Document
Column = Field
Table Join = Embedded Documents
Primary Key = Default key _id provide by mongodb itself

MongoDB language is based on JSON Language. No Join / No complex transaction.

JSON

Example : Document/Row
{ _id :"A11",
x :"1",
y:"2",
z:{a,b} --embedding
}

Find id = A11

Example: query

db.collection.find({_id: "A11"})

JSON Types (Java Script Object Notation)

- Pure data exchange format
- Document-Oriented















Quiz : How many data types in Json ?

Ans : 6

  1. Strings
  2. Numbers
  3. Booleans(True\False)
  4. Null
  5. Arrays
  6. Objects/Documents
















Left hand side - Only String is a valid type

Quiz

















Ans:

Person
{"name" : "John",
  "Age" : 25,
 "Address" :
  {"city" : "New York",
     "PostalCode" : 10021
 }
"Phone" :
{"Home": "212-555-1234",
"Mobile": "646-555-1234"
}
}

Binary JSON

Fast Scannability
Data types

  1. Binary Data
  2. Object ID
BSON Format
-- Mongodb driver
















integer = 32 bit (4 bytes)

BSON and Applications
















Friday, September 26, 2014

Starting MONGODB - NoSQL

I heard about MongoDB from a friend and wanted to try out open source database.

Installing Mongo DB

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

I am installing MongoDB on windows 7. I have downloaded windows version from Mogodb website and unpacked msi.


  • move bin folder to C:\MogoDB\
  • create a directory for data - c:\data\db
  • Setting data path
  • c:\mongodb\bin\mongod.exe --dbpath c:\data\db





Starting MongoDB service

Open a command prompt

C:\MongoDB\Bin\Mongod.exe


Waiting for connections on port 27017 means MongoDB service is successfully started.



Open another command Prompt to connect to Mongo Database

Type

C:\mongodb\bin\Mongo.exe

You will see the error that cannot find the file mongorc.js



So you tell the shell to ignore looking for .mongorc.js

C:\MongoDB\Bin\Mongo.exe --norc --shell



Now you are successfully connected to database.

type 

db
it will return test, connecting to test database





use mydb


show collections



Generate test data in a loop


Read inserted data




It was fun to play with MongoDB. More posts will be coming soon...

How to add a Database to AlwaysOn Availability Group with four different options

To add a database to an existing AlwaysOn availability group, MS has given us four options to choose from Automatic seeding Full database an...