mongo saran&sathya
TRANSCRIPT
![Page 1: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/1.jpg)
SathyaNarayanan & Saravanan
open-source, high-performance, document-
oriented database
![Page 2: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/2.jpg)
mongoDBMongoDB is:
Powerful Flexible Scalable data store Secondary indexes Range queries Sorting Built-in support for MapReduce-style aggregation and Geospatial indexes.
![Page 3: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/3.jpg)
mongoDBMongoDB has
A developer-friendly data model
Administrator-friendly configuration options
Natural-feeling language APIs presented by drivers and the database shell.
![Page 4: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/4.jpg)
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.
![Page 5: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/5.jpg)
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.
![Page 6: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/6.jpg)
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.
![Page 7: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/7.jpg)
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.
![Page 8: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/8.jpg)
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.
![Page 9: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/9.jpg)
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
![Page 10: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/10.jpg)
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
![Page 11: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/11.jpg)
mongoDB
MongoDB Terminology:
row document table collectiondatabase database
![Page 12: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/12.jpg)
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".
![Page 13: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/13.jpg)
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
![Page 14: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/14.jpg)
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
![Page 15: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/15.jpg)
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
![Page 16: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/16.jpg)
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
![Page 17: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/17.jpg)
mongoDBInstallation :
Nothing to install. Its very easier just extract the Zipped content we will get a hierarchy as shown!!
![Page 18: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/18.jpg)
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
![Page 19: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/19.jpg)
mongoDB
For our convenience we will have it in our desktop
![Page 20: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/20.jpg)
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
![Page 21: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/21.jpg)
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”
![Page 22: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/22.jpg)
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
![Page 23: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/23.jpg)
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
![Page 24: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/24.jpg)
mongoDBStarting mongo client: ./soft/mongodb-osx-x86_64-1.7.4/bin/mongo
By default it will connect to the “test” DB
![Page 25: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/25.jpg)
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
![Page 26: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/26.jpg)
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
![Page 27: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/27.jpg)
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
![Page 28: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/28.jpg)
mongoDB
![Page 29: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/29.jpg)
mongoDBIn Mongo all the commands inside the database are functions>db.xxx() so in case of help just type >db.help()
![Page 30: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/30.jpg)
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
![Page 31: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/31.jpg)
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}
![Page 32: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/32.jpg)
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
![Page 33: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/33.jpg)
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
![Page 34: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/34.jpg)
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
![Page 35: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/35.jpg)
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..
![Page 36: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/36.jpg)
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..
![Page 37: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/37.jpg)
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
![Page 38: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/38.jpg)
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
![Page 39: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/39.jpg)
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 } );
![Page 40: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/40.jpg)
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
![Page 41: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/41.jpg)
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 } ] } )
![Page 42: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/42.jpg)
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
![Page 43: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/43.jpg)
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
![Page 44: Mongo Saran&Sathya](https://reader036.vdocuments.mx/reader036/viewer/2022062503/577cc9be1a28aba711a47f59/html5/thumbnails/44.jpg)
mongoDB
Thank you