mongo saran&sathya
TRANSCRIPT
SathyaNarayanan & Saravanan
open-source, high-performance, document-
oriented database
mongoDBMongoDB is:
Powerful Flexible Scalable data store Secondary indexes Range queries Sorting Built-in support for MapReduce-style aggregation and Geospatial indexes.
mongoDBMongoDB has
A developer-friendly data model
Administrator-friendly configuration options
Natural-feeling language APIs presented by drivers and the database shell.
mongoDBMongoDB is Document-oriented
to replace the concept of a “row” with a more flexible model, the “document”.
By allowing embedded documents and arrays, the document-oriented approach makes it possible to represent complex hierarchical relationships with a single record.
This fits very naturally into the way developers in modern object-oriented languages think about their data.
mongoDBMongoDB is Schema-free
A document’s keys are not predefined or fixed in any way. Without a schema to change, massive data migrations are usually unnecessary. New or missing keys can be dealt with at the application level, instead of forcing all data to have the same shape. Gives developers a lot of flexibility in how they work with evolving data models.
mongoDBMongoDB is High Performance
Scaling a database : Scaling up or Scaling outMongoDB - Designed to scale out.Its document-oriented data model allows it to automatically split up data across multiple servers.It can balance data and load across a cluster, redistributing documents automatically. When more capacity is needed, just adding new machines to the cluster is enough.
mongoDBMore Features of MongoDB:
IndexingMongoDB supports generic secondary indexes, allowing a variety of fast queries, and provides unique, compound, and geospatial indexing capabilities as well.
Stored JavaScriptInstead of stored procedures, developers can store and use JavaScript functions and values on the server side.
mongoDBMore Features of MongoDB:
AggregationIt supports MapReduce and other aggregation tools.
Fixed-size collectionsCapped collections are fixed in size and are useful for certain types of data, such as logs.
File storageMongoDB supports an easy-to-use protocol for storing large files and file metadata.
mongoDBMore Features of MongoDB:
MongoDB uses a binary wire protocol as the primary mode of interaction with the server .It adds dynamic padding to documents and preallocates data files to trade extra space usageIt uses memory-mapped files in the default storage engine, which pushes the responsibility for memory management to the operating system.It features a dynamic query optimizer that “remembers” the fastest way to perform a query
mongoDBMongoDB has Simple Administration:
Very little administration is necessary
If a master server goes down, MongoDB can automatically failover to a backup slave and promote the slave to a master
In a distributed environment, the cluster needs to be told only that a new node exists
mongoDB
MongoDB Terminology:
row document table collectiondatabase database
mongoDBMongoDB Terminology:
row document
JSON-style Documents represented as BSON
BSON is a binary-encoded serialization of JSON-like documents. BSON is designed to be lightweight, traversable, and efficient. BSON, like JSON, supports the embedding of objects and arrays within other objects and arrays.
MongoDB uses BSON as the data storage and network transfer format for "documents".
mongoDBUse mongoDB: BSON in Brief
Type PrefixDouble \x01String \x02
Integer(32)
\x10
Long(64) \x12ObjectId \x07
{“hello”: “world”}
\x16\x00\x00\x00 length\x02 property typehello\x00 property name\x06\x00\x00\x00world\x00 property value\x00 a EOF
mongoDBMongoDB Terminology:
table collectionCollections are schema-free.
{"greeting" : "Hello, world!"} {"foo" : 5}
why should we use more than one collection?Keeping different kinds of documents in the same collection can be a nightmare for developers and admins.It is much faster to get a list of collections than to extract a list of the types in a collection.Grouping documents of the same kind together in the same collection allows for data locality.index our collections more efficiently
mongoDBMongoDB Terminology:
database database
MongoDB groups collections into databases. A single instance of MongoDB can host several databases, each of which can be thought of as completely independentA database has its own permissions, and each database is stored in separate files on diskSeparate databases are useful when storing data for several application or users on the same MongoDB serverThere are also several reserved database names, which you can access directly but have special semantics
adminlocalconfig
mongoDBScalable, Schema less, Document Oriented,High availability Open source Database. Written in C++ Features:
Full index support including compound keys
De-Normalized ( No-Joins )
mongos: automatic shardingkeysreplica set: automatic failover
mongoDBInstallation :
Nothing to install. Its very easier just extract the Zipped content we will get a hierarchy as shown!!
mongoDBMongo expects a folder structure to store its data Contents. as : "/data/db”
By default it expects in the root directory. So we have to create a folder structure in the root directory .
orWe can have the folder structure at our desired location. But we have to direct the Mongo to the corresponding location while starting the server
mongoDB
For our convenience we will have it in our desktop
mongoDBStarting the server “mongod”:
In the terminal run the following command if “data/db” is provided in the root directory
./soft/mongodb-osx-x86_64-1.7.4/bin/mongod
else
We have to specify the - -dbpath option and its path while starting the server
mongoDB./soft/mongodb-osx-x86_64-1.7.4/bin/mongod --dbpath /Users/saravananprabhakaran/Desktop/MongoDb-Works/
Note: we have to point dbpath t0 parent directory of “data/db”
mongoDBStarting MongoDB server by default listening on port: 27017
In order to change the port while starting include--port 12345as./soft/mongodb-osx-x86_64-1.7.4/bin/mongod --dbpath /Users/saravananprabhakaran/Desktop/MongoDb-Works/ --port 12345
mongoDBLogging: By default its not enabled.
In order to enable it start the server with the following option:- --fork --logpath location/filename.log--logappend
as./soft/mongodb-osx-x86_64-1.7.4/bin/mongod --dbpath /Users/saravananprabhakaran/Desktop/MongoDb-Works/ --fork --logpath /Users/saravananprabhakaran/Desktop/MongoDb-Works/mongodb.log --logappend
mongoDBStarting mongo client: ./soft/mongodb-osx-x86_64-1.7.4/bin/mongo
By default it will connect to the “test” DB
mongoDBCreating an user account:
./soft/mongodb-osx-x86_64-1.7.4/bin/mongo -u saran -p saran007 admin
Creating a new database:use saranDB
Switch to admin Add an user with required user name & passwordQuit the client & re-login with the new user created
mongoDBCreating an user account & Database:
>use admin
>db.addUser('saran', 'saran007')
./soft/mongodb-osx-x86_64-1.7.4/bin/mongo -u saran -p saran007 admin
Switch to admin Add an user with required user name & passwordQuit the client & re-login with the new user created
mongoDBDatabase:
>use saranDB
If exists will switch to the saranDB else will create new and switch to it
To list all the database >show dbs
If in case of any help. Just type help in the terminal
>help
Use database_Name
mongoDB
mongoDBIn Mongo all the commands inside the database are functions>db.xxx() so in case of help just type >db.help()
mongoDBIf we want to know about any predefined functions justtype
>db.functionName with out brackets
It will explain what is expected as input and what will be the return type
mongoDBData types:
Null:Null can be used to represent both a null value and a nonexistent
field. Ex: {"x" : null}
Boolean:There is a boolean type, which will be used for the values 'true' and
'false’. Ex: {"x" : true}
64-bit floating point number:All numbers in the shell will be of this type. Thus, this will be a
floating-point number. Ex: {"x" : 3.14}
As will this: {"x" : 3}
mongoDBData types continues…
String:Any string of UTF-8 characters can be represented using the string
type Ex: {"x" : "foobar"}
Arrays: Sets or lists of values can be represented as Arrays Ex: {"x" : ["a", "b", "c"]}
Embedded document:Documents can contain entire documents, embedded as values in a
parent document:{"x" : {"foo" : "bar"}}
Objects: Special type like associative array Ex: saran[PG]=MCA
mongoDB
In Mongo we call schemas as collections
How to create a schema?..
>db.saran.save({ “name”:”saravanan”});
Name of the collection
Field Name Value
This will insert a document in Mongo, to view it
>db.saran.find(); to view all
>db.saran.findOne(); to view Only one
mongoDB
_id and ObjectIds:
Every document stored in MongoDB must have an "_id" key. The "_id" key’s value can be any type, but it defaults to an ObjectId. In a single collection, every document must have a unique value for "_id", which ensures that every document in a collection can be uniquely identified. That is, if you had two collections, each one could have a document where the value for "_id" was 123.
However, neither collection could contain more than one document where "_id" was 123
mongoDB
> j = { name : "mongo" };{"name" : "mongo"}
> t = { x : 3 };{ "x" : 3 }
> db.things.save(j);>db.things.save(t);
> db.things.find();{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
Various ways of storing..
mongoDB
>for (var i = 1; i <= 30; i++) db.things.save({x : 4, j : i});
> db.things.find();or> var cursor = db.things.find();>while (cursor.hasNext()) printjson(cursor.next());
20 records can only be listed at a time if want to view next 20 then uses iterate command
>it
Loops..
mongoDB
> var cursor = db.things.find();> printjson(cursor[4]);
or
If you use toArray() we can directly print the indexed content with out using JSON
> var arr = db.things.find().toArray();> arr[4];
Array indexing on result-set rather than iterating
mongoDB
We want to print those records having name=Mongo
> db.things.find({name:"mongo"});
equivalent sql query SELECT * FROM THINGS WHERE name='mongo';
Want to print selective field not all
Fetching the records using condition
whether we have where condition or not we have to put dummy curly braces in its place as> db.things.find( { }, { j:true} );
the field which ever has to be displayed has to mentioned as true specifically, but after where condition
mongoDB
What If in a table except one field all fields should be displayed in a Result-Set Then that field alone should be marked as false
> db.saran.find({x:4},{j:false});
what if in case you want to display a particular field which is present with in collection of collectionSay like this:{ "_id" : ObjectId("4cfb1d1d3eecf4b69094159b"), "name" : "MongoDB", "type" : "database", "count" : 1, "info" : { "x" : 203, "y" : 102 } }
In that case use : > db.saran.find( { } , { "info.x":true } );
mongoDB
Functions..
limit (number) -- Restricts the No: of records in the output
> db.saran.find().limit(3);
Count() -- Gives the collection size
> db.saran.count();
skip (number) -- skips those No: of records before reading
> db.saran.skip();
sort () -- ascending / descending
db.saran.find({age:33}).sort({name:1}) ascending order
db.saran.find({age:33}).sort({name:-1}) descending order
mongoDB
db.saran.find( {age: {$gt:23 , $lt:26} } );
Range:
db.users.distinct('last_name')Distinct:
db.users.find( { a:1 , b:'q' } );
AND & OR:
db.saran.find( { x:{$exists:true} } )exists:
db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
mongoDB
Map Reduce:
Rather than dealing with slides lets make our hand dirty with some hands-On for better understanding
Refer to the attached MapReduce.txt
mongoDB
Connecting with Java
Rather than dealing with slides lets make our hand dirty with some hands-On for better understanding
Refer to the MongoAPI.java
mongoDB
Thank you