How does MongoDB help you manage a huMONGOus amount of data collected through your web application? With this authoritative introduction, you'll learn the many advantages of using document-oriented databases, and discover why MongoDB is a reliable, high-performance system that allows for almost infinite horizontal scalability. Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for system administrators, and an overview of the concepts and use cases for other people on your project. Learn how easy it is to handle data as self-contained JSON-style documents, rather than as records in a relational database.
The main concepts are well explained but as any technical book focused on a tool that evolves quickly it get out-dated quickly as well. The main concepts are still valid but some details have already changed sometimes significantly, it is advisable to read it with the current documentation on the side to be aware of the differences.
- The basic idea is to replace the concept of a “row� with a more flexible model, the “document.� - MongoDB was designed from the beginning to scale out. Its document-oriented data model allows it to automatically split up data across multiple servers
Features : a) Indexing b) stored Javascript c) Agreggation d)Fixed-size collections e)File storage
Chapter 2 : Gettting Started
� A document is the basic unit of data for MongoDB, roughly equivalent to a row in a relational database management system (but much more expressive). � Similarly, a collection can be thought of as the schema-free equivalent of a table. � A single instance of MongoDB can host multiple independent databases, each of which can have its own collections and permissions. � MongoDB comes with a simple but powerful JavaScript shell, which is useful for the administration of MongoDB instances and data manipulation. � Every document has a special key, "_id", that is unique across the document’s collection.
- A document is an ordered set of keys with associated values. Concepts on a document : - Key/value pairs in documents are ordered - Values in documents are not just “blobs.�
MongoDB is type-sensitive and case-sensitive.
- The shell is a full-featured JavaScript interpreter, capable of running arbitrary JavaScript programs and you can even defined JS functions - The real power of the shell lies in the fact that it is also a stand-alone MongoDB client
Insert :
post = {"title" : "My Blog Post", "content" : "Here's my blog post.", "date" : new Date()}
db.blog.insert(post) ( Insert in the collection blog the post object) db.blog.find() - return all the documents in a collection Update update takes (at least) two parameters: the first is the criteria to find which document to update, and the second is the new document.
Detele db.blog.remove({title : "My Blog Post"}) - remove the document that has the title “My Blog Post�
Help on commands : A good way of figuring out what a function is doing is to type it without the parentheses.
Basic data types On the other hand, JSON’s expressive capabilities are limited, because the only types are null, boolean, numeric, string, array, and object.
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.
Chapter 3 : Creating, Updating, and Deleting Documents
- $push" adds an element to the end of an array if the specified key already exists and creates a new array if it does not - Updates, by default, update only the first document found that matches the criteria. If there are more matching documents, they will remain unchanged. To modify all of the documents matching the criteria, you can pass true as the fourth parameter to update. - The same getLastError command that powers safe mode also contains functionality for checking that operations have been successfully repli- cated.
Chapter 4 : Querying
1) collection.find() - get all the documents from a collection 2) collection.find({"age" : 27}) - get all the document that have the age 27 3) collection.find({"age" : 27},{"username" : 1, "email" : 1}) - get all the documents only the keys username and email 4) collection.find({"age" : 27},{"username" : 0 , “_id� :0 }) - return all except username and id
Query Criteria
comparison operators : "$lt" : < "$lte" : <= "$gt" : > "$gte" : >= Ex : > start = new Date("04/04/2007") > db.users.find({"registered" : {"$lt" : start}}) - show all the registered users before 04 04 2007
- "$ne" : which stands for “not equal.� > db.users.find({"username" : {"$ne" : "joe"}}) - show all users that aren’t called joe
OR Queries -can be user �$or� or �$in� ( "$nin" is the opposite of �$in� } db.cinema.find({"seat" : {"$in" : [7, 42, 90]}})
"$not" is used to negate a statement
If we only want to find keys whose value is null, we can check that the key is null and exists using the "$exists" conditional: > db.c.find({"z" : {"$in" : [null], "$exists" : true}})
Regular expressions : > db.users.find({"name" : /joey?/i}) - Find all the users that are joey ( case insensitive)
Limits, Skips, and Sorts > db.c.find().limit(3) - return only 3 results > db.c.find().skip(3) - will skip the first three matching documents and return the rest of the matches > db.c.find().sort({username : 1, age : -1}) sort the results by "username" ascending and "age" descending
Chapter 5 - Indexing
- MongoDB’s indexes work almost identically to typical relational data- base indexes - To create the index, use the ensureIndex method: Example : if we want to index on the people > db.people.ensureIndex({"username" : 1}) - If you have more than one key, you need to start thinking about index direction. > db.status.ensureIndex({user : 1, date : -1}) ( query by user and date to pull up all of a user’s recent statuses ) - Indexes can be created on keys in embedded documents in the same way that they are created on normal keys. - Unique indexes guarantee that, for a given key, every document in the collection will have a unique value. > db.people.ensureIndex({"username" : 1}, {"unique" : true}) - explain will return information about the indexes used for the query (if any) and stats about timing and the number of documents scanned. > db.people.find().explain()
> db.people.ensureIndex({"username" : 1}, {"background" : true}) Building indexes is time-consuming and resource-intensive. Using the {"background" : true} option builds the index in the background, while handling incoming requests. If you do not include the background option, the database will block all other requests while the index is being built. - you can remove it with the dropIndexes command and the index name. > db.runCommand({"dropIndexes" : "foo", "index" : "alphabet"})
Geospatial Indexing A geospatial index can be created using the ensureIndex function, but by passing "2d" as a value instead of 1 or -1: > db.map.ensureIndex({"gps" : "2d"}) The "gps" key must be some type of pair value, that is, a two-element array or embedded document with two keys. These would all be valid: { "gps" : [ 0, 100 ] } { "gps" : { "x" : -30, "y" : 30 } } { "gps" : { "latitude" : -180, "longitude" : 180 } }
For example, the following code returns the 10 nearest documents to (40, -73) coordinates: > db.map.find({"gps" : {"$near" : [40, -73]}}).limit(10)
MongoDB also allows you to find all of the documents within a shape, as well as near a point.There are two options: you can query for all points within a rectangle or a circle. To use a rectangle, use the "$box" option: > db.map.find({"gps" : {"$within" : {"$box" : [[10, 20], [15, 30]]}}}) "$box" takes a two-element array: the first element specifies the coordinates of the lower-left corner, the second element the upper right. Also, you can find all points within a circle with "$center", which takes an array with the center point and then a radius: > db.map.find({"gps" : {"$within" : {"$center" : [[12, 25], 5]}}})
Chapter 6 - Aggregation
- count > db.foo.count()
count the number of results for that query: > db.foo.insert({"x" : 2}) distinct command finds all of the distinct values for a given key. You must specify a collection and key: > db.runCommand({"distinct" : "people", "key" : "age"}) group - similar to group by > db.runCommand({"group" : { ... "ns" : "stocks", ... "key" : "day", ... "initial" : {"time" : 0}, ... "$reduce" : function(doc, prev) { ... if (doc.time > prev.time) { ... prev.price = doc.price; ... prev.time = doc.time; ... } ... }}})
Chapter 7 - Advanced Topics
- getLastError command to check the number of documents af- fected by an update:
Drop a collection : db.test.drop() or db.runCommand({"drop" : "test"});
Capped Collections : - fixed in size - capped collections automatically age-out the oldest documents as new documents are inserted. - Documents cannot be removed or deleted (aside from the automatic age-out described earlier), and updates that would cause documents to move (in general updates that cause documents to grow in size) are disallowed. - capped collections ideal for use cases like logging > db.createCollection("my_collection", {capped: true, size: 100000, max: 100}); > db.runCommand({convertToCapped: "test", size: 10000}); - converts a regular collection to a capped
db.my_collection.find().sort({"$natural" : -1}) - sort in reverse insertion order with a natural sort
GridFS is a mechanism for storing large binary files in MongoDB. - can store Javascript - DBRef is an embedded document, just like any other embedded document in MongoDB. A DBRef, however, has specific keys that must be present. A simple example looks like the following: {"$ref" : collection, "$id" : id_value}
Chapter 8 - Administration addUser method is useful for more than just adding new users: it can be used to change a user’s password or read-only status. Just call addUser with the username and a new password or read-only setting for the user.
Backup:
> ./mongodump -d test -o backup
Restore from backup ./mongorestore -d foo --drop backup/test/
Chapter 9 - Replication
Master-slave replication is the most general replication mode supported by MongoDB. This mode is very flexible and can be used for backup, failover, read scaling.
Chapter 10 - Sharding
Sharding is MongoDB’s approach to scaling out. Sharding allows you to add more machines to handle increasing load and data size without affecting your application.
Chapter 11 Example Applications - App : Chemical Search Engine in Java - App : News Aggregator in PHP - App : Custom Submission Forms in Ruby - App : Real-Time Analytics in Python
Average book. Covers only basics, unnecessarily describes things easily found elsewhere (like arguments of mongodump, or mongo commands). Often explains obvious things. Recommended only for very beginners.
A very detailed coverage of all MongoDB aspects - development, administration, monitoring and performance-tuning - accompanied with lots of pearls of wisdom and typical gotchas.
The book serves as a good tutorial covering the fundamentals of Mongo. It pretty well describes clustering (replicas and sharding) and recommended hierarchies but does not tell much about schema design and does not give you a hint of how to utilize the benefits of NoSQL databases like dynamic schema per document. The book serves as a very good tutorial, but there are several minor issues.
The book states that it is over 500 pages long, but actually, it is not that long. It has 24 chapters, there are a lot of empty pages at the end of chapters and parts and there are a lot of pages wasted by listing some command outputs. Most of them can be stripped significantly without affecting provided information. For example, there is command output starting at page 348 which spans over 5 full pages and actually only a few fields of this listing are referenced in explaining text.
I do not consider the book comprehensive. While it mentions a lot of things, the author does not provide extensive listings of features. For example, the book mentions how to use the operator plus, and they mention that other “common� operators work similarly, but they do not list them. To properly learn MongoDB, you will need to find them in the documentation. This happens to many other topics described in the book.
The book few times refers to terms which are not described at all or are only briefly described.
Lastly, I did not find data for experiments in Chapter 7 on GitHub and actually, it is the chapter which does the most complex queries against data.
To sum it up, it is a good book for learning MongoDB, but it most probably will not be your only learning material and there are a few mentioned minor issues, so I give it a 4-star rating.
The book was very comprehensive. I think it will be very useful for system administrators managing MongoDB as half of the book describes replication and sharding and also how to setup networking and so. I would want more information about how MongoDB works under the hood and how you optimize your queries and data structures for efficiency.
This book covers indexing, querying, schema design, sharding, replication, scalability, application design, and more. However, it does not cover too much about its storage engines. This is only briefly mentioned at the end of the book. Still, this book is comprehensive in scope and really good.
Sorry, this review is in german. I do not have enough time to translate it into english. In a few years this probably will be automatically translated. ------------------------------------------------------------ Das Buch ist ein gelungener Überblick auf MongoDB sowohl aus Entwickler- als auch als DBA-Perspektive. Jedenfalls soweit ich das als MongoDB-Einsteiger mit mehr als 11 Jahren Data-Warehousing-Projekterfahrung sagen kann. Es ist mein erstes Buch über MongoDB und man kann es daher auch als Einstieg benutzen.
Warum nur vier Sterne?
- Ich hätte gerne das erlernte Wissen anhand von Aufgaben überprüft. Hier fehlen Beispielaufgaben. - Mir fehlt auch ein Überblick über das gesamte MongoDB-Ökosystem und die verschiedenen Zusatzprogramme, die inzwischen erhältlich sind, wie z. B. Admin UIs, IDEs, evtl. sogar die Integration mit Hadoop. - Auch gibt es ein paar Unstimmigkeiten, wie z. B. funktionieren ein paar Beispiele nicht.
Alles in allem, ein gelungenes Buch. Ob es wirklich “definitiv� im Sinne von endgültig ist, wird sich allerdings erst bei der Lektüre von anderen Büchern zeigen.
Overall the book is well written, the two authors clearly know their stuff. However, I wish that they have organized the topics and wording a bit clearer and not to expect people will read the book from cover to cover.
MongoDB, at the end of the day, is a very young package. So there are quirks here and there which don't necessarily make sense for people from the RDBMS paradigms. E.g. it probably make sense to reference Safe Operations earlier in chapter 3 because I know that there are folks out there who will drop using MongoDB mid-way through chapter 3.
At the end of each chapter, it would be good to have a summary table of that chapter. Like a list of commands, APIs, parameters, and links to the latest on MongoDB's website. Chapter 3 and 4 will get the most out of this.
In closing, I would still recommend this book to those who's not able to learn or understand MongoDB from online documentation.
By the title of the book that is The Definitive Guide, I was hoping that the authors could point out some kind of work patterns that could be considered as a starting point for the readers to be used as their practical guidelines at work. Although, I am aware that it is still too early to consider best practices to work with MongoDB.
The documentations provided at mongodb.org are definitely self-explanatory and I believe I could learn much faster from the documentations.
I am a bit disappoint actually, that's why I rated this book as an OK book. Much of the information could already be found in the mongodb.org documentations.
Ottimo manuale. Parte dalle basi e affronta tutti gli argomenti, dal classico "CRUD" all'amministrazione del server, passando per replicaSets, sharding, backup ed esempi in Java, PHP, Python e Ruby. Volendo assolutamente trovargli dei difetti direi che con un titolo come "Definitive Guide" mi sarei aspettato un maggiore approfondimento per alcuni argomenti, come i riferimenti incrociati tra collection, l'uso avanzato dello storage di file o degli esempi di configurazioni reali per sistemi in replicaSet + sharding.
The last third of the book is appendices, so apparently all the while I was doner than I thought I was. It's a great book, but it's short, and therefore I wonder if has proper claim to the title of "Definitive Guide." My only complaint is that the Java chemistry app example in the last chapter is not accessible for anyone who didn't major in the physical sciences or who has been out of school for more than a few years.
I recommend reading this book if you are interested in Mongo specifically, or even document databases in general. I found it to be a very useful introduction. I came to the book with some basic knowledge of document database, but only a basic knowledge. I am now comfortable working with Mongo (those still only an intermediate user at best).
Great book and introduction to MongoDB - a must read if you are journeying into the land of NOSQL databases. I love Mongodb but coming from a relational background there are many things to learn about this JSON / BSON document based datastore. Great overview but you will need other books to get in much deeper with this product.
Around 200 pages, it is concise enough for an introductory book. The introduction does an excellent job of introducing the concepts behind MongoDB. It goes into explaining the pros and cons of NoSQL engines. Administration, implementation and development concepts are covered. Maybe a little too much focused on a developer's point of view.
is useful reading and starting point for those interested to touch MongoDB. The book is well composed with enough clear samples and explanations. I've enjoyed reading it.
Excellent book, great details of all things related to Mongo. I would not recommend it for beginners (as myself) because there is plenty of things that will become much more relevant once you have an application in production. If you read the book before that, there's a great chance that you will forget and will have to come back later and reread it.
This book does a great job on covering every aspect of MongoDB - I especially like the chapters on server deployment and administration, which are often an afterthought with this kind of book.
The chapter on application design was too short for my taste. I would have liked to see examples of good application design and more common patterns here.
This book is a fairly good read for a technology book. Most of the content is platform-agnostic, so it's accessible to anyone thinking about using MongoDB on a platform that has a MongoDB driver already.
Book generally written much of a theoric book but section at the end provide enough practice for different language drivers (java,ruby,python etc.) but one important thing that they are not full examples
My main criticism of this book is that examples are not set up properly and none is worked out in any detail. Not bad as a whistle-stop tour of features, but online documentation is a better introduction to MongoDB.