2. Today’s Talk
• Idea about mongoDB , mongo shell
• CRUD operation of mongo i.e.Insert,Update,Delete.
• Introduction to findOne, Querying using field
selection,$ gt,$ lt
• Using regexes,$ exists, $ type,$ or,$ and,$ in,$ all
• Index in mongoDB.
• Aggregation in mongoDB.
3. What is MongoDB?
• Scalable, High-Performance, Open-source.
• Document Oriented Database
• Built for Speed.
• Full Index Support for High Performance.
• Replication and Failover for High Availability.
4. MongoDB
• Map / Reduce for Aggregation
• Auto Sharding for Easy Scalability
• Platforms 32/64 bit Windows Linux, Mac OS-X,
FreeBSD, Solaris
• Language drivers for:
– Ruby / Ruby-on-Rails
– Java
– C#
– JavaScript
– C / C++
– Node.js
– Erlang Python, Perl others..... and much more ! ..
5. • No Joins + No multi-row transactions
– Horizontally Scalable
• Built-in Replication
–Easy to configure
–Automatic failover when using Replica Sets
Fast read/write operation
Shard for scalability, replicate for availability
Why MongoDB:- Scalability / Availability /
Performance
9. BSON
• JSON has powerful, but limited set of datatypes
–Mongo extends datatypes with Date, Int types, Id, …
• MongoDB stores data in BSON
• BSON is a binary representation of JSON
26. group()
db.article.aggregate(
{ $group : {
_id : "$author",
docsPerAuthor : { $sum : 1 },
viewsPerAuthor : { $sum : "$pageViews" }
}}
);
This aggregation pipeline groups by the author field and computes two fields, the
first docsPerAuthor is a counter field that increments by one for each document
with a given author field using the $sum function. The viewsPerAuthor field is the
sum of all of the pageViews fields in the documents for each group.
29. explain()
explain()
explain will return information about the indexes used for the query (if any)
and stats about timing and the number of documents scanned.
db.users.find({"name":"user0"}).explain()
{
"cursor" : "BtreeCursor name_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"name" : [
[
"user0",
"user0"
]
]
}
}
31. Summary
• MongoDB is good
-Stores objects.
-Felxible semi-structured design
-Store big data
-Embedded documents to eliminates join.
• Mongodb is bad
-No multi-document query
-No Support for transactions