Learn about document oriented databases. This video focuses on JSON document style.
- [Instructor] In a document database, objects are stored with all of their related information included. A document in a Mongo database is simply a JSON object. The data is actually stored in a related binary format known as BSON, but all of your interactions with the data will use JSON, regardless of the language or driver you use. Storing the data this way makes it much easier to access related information. Searching on specific attributes of an object or sorting items based on a field works naturally and quickly.
Indices can be created across different fields in the collection documents. Working with JSON, a common format, makes it easy to adapt the data to your personal development environment. But what does a MongoDB item look like? In the most basic form, let's look at the user we created earlier. When I inserted the object, it looked like this. This is a flat object with no nested items, a very simple JSON representation of this resource. You may have noticed that the object returned by Mongo had an additional field, the _id field.
Every item in a Mongo database has this field. If it's not specified at the time the object is created, one will be assigned by Mongo. This field must be unique across a specific collection, but otherwise can be any identifying string you wish to use if you don't want to work directly with the Mongo IDs. Just like the items within the database or documents, the query language used by Mongo relies heavily on documents to know what objects to operate on. For instance, the find command here instructs Mongo to find all of the items matching the first name of John.
It does this by passing in a simple JSON document defining first_name as John. The pattern holds true for other MongoDB queries as well, both in the shell and in the Mongo drivers. Updates, sorts and deletes all use documents to express how the operation should work with the data in the system. This remove command removes all items in the collection with the first name of John. A documents is set, and the operation happens on the items matching that document. When we rerun the request to find all the John entries, we get no results, which is what we'd expect.
The update seen here updates all of the items in the database with John as the first name and sets the value specified here. Note, the update is a complete overwrite. If you had left out the address and phone, John would only have first and last names. The original update could have been done with the set operator, and this operation leaves all of the remaining fields in place. It's likely that you'll use set much more often than a bare update, because most changes to a database are additive and adding new fields can easily be done with this operation.
Mongo documents also support nested objects, objects which are deeper than a single set of keys and values. Here, we're giving John multiple phone numbers represented as an array in the data. With this simple array, we can do a search to find our matching items, which is really quite cool. And Mongo supports nesting up to 100 levels. You're unlikely to need anything remotely close to that, but how can you access exactly what you want in a richly defined object? While we'll dive into this more deeply later, here are a few examples of the types of interactions you could do with this data.
Find the users with the matching fax number and return just the phone number. Find the user with the matching nested phone number. Note, we'll learn more about the dot notation you see here as we go through the query section. And finally, give me just the phone numbers for everyone in the database.
- Installing MongoDB for Windows and OS X
- Why Mongo?
- Document-oriented data
- Exploring the Mongo shell
- Importing data into the database
- Building an application in Node.js
- Tuning Mongo queries
- Replication and sharding