Monday, 10 June 2013

The Basics of MongoDB

Document for Junior level DBA’s
The Basics of MongoDB

We begin our journey by getting to know the basic mechanics of working with MongoDB. Obviously this is core to
understanding MongoDB, but it should also help us answer higher-level questions about where MongoDB fits.
To get started, there are six simple concepts we need to understand.
1. MongoDB has the same concept of a database with which you are likely already familiar (or a schema for you Oracle or SQL Server guys).
Within a MongoDB instance we can have zero or more databases, each acting as high-level containers.

2. A database can have zero or more collections. A collection shares enough in common with a traditional table that you can safely think of the two as the same thing.

3. Collections are made up of zero or more documents. Again, a document can safely be thought of as a row.

4. A document is made up of one or more fields, which you can probably guess are a lot like columns.

5. Indexes in MongoDB function much like as RDBMS concepts.

6. Cursors are different than the other five concepts but they are important enough, and often overlooked, that I think they are worthy of their own discussion. The important thing to understand about cursors is that when you ask MongoDB for data, it returns a cursor, which we can do things to, such as counting or skipping ahead, without
actually pulling down data.

To recollect, MongoDB is made up of databases which contain collections. A collection is made up of documents.
Each document is made up of fields. Collections can be indexed, which improves lookup and sorting performance.
Finally, when we get data from MongoDB we do so through a cursor whose actual execution is delayed until necessary.



The difference s between those relational databases define columns at the table level whereas a document-oriented database defines its fields at the document level.

We can say the document within a collection can have its own unique set of fields.

As such, a collection is a dumbed down container in comparison to a table, while a document has a lot more information than a row.
More over, the point is that a collection is strict about what goes in it (schema-less). Fields are tracked with each individual document.

Let’s get hands-on. If you don’t have it running already, go ahead and start the mongod server as well as a mongo shell.
The shell runs JavaScript. There are some global commands you can execute, like help.

Commands that you execute on current database are executed against the db object, such as or db.stats()

Commands that you execute against a specific collection, which is what we will be doing a lot of, are executed against the db.COLLECTION_NAME object,
such as db. or db.goodhealth.count()

Go ahead and enter, displays list of commands that you can execute on the database objects.

Note: This is a JavaScript shell, if you execute a method and omit the parentheses (), you will see the method body rather than executing the method.

I only mention it because the first time you do it and get a response that starts with function (...).

For example, if you enter (without the parentheses), we can see the internal implementation of the help method.

After connecting to mongo shell means you connected with a test database , then you can start issuing database commands, like db.getCollectionNames()
If you do so, you should get an empty. Since no collections are schema-less then we needs to create.

We can simply insert a document into a new collection. To do so, use the insert command, supplying it with the document to insert:
db.goodhealth.insert ({name: "SerenTom", gender: "f", weight: 50})
db.goodhealth.insert( { item: "postcard", qty: 25 } )

The above line is executing insert against the goodhealth collection, passing it a single argument.

Internally MongoDB uses a binary serialized JSON format. this means that we use JSON a lot, as is the case with our parameters.

If we execute db.getCollectionNames(), we see some collections: goodhealth and system.indexes.

The collection system.indexes is created  once per database and contains the information on our database’s index.
You can now use the find command against goodhealth collection to return a list of documents:

Note that, in addition to the data you specified, there is an _id field. Every document must have a unique _id field.

You can either generate one yourself or let MongoDB generate an ObjectId for you.

By default, the _id field is indexed - which explains why the system.indexes
collection was created.
You can have a look at system.indexes: db.system.indexes.find()

What you are seeing is the name of the index, the database and collection it was created against and the fields included in the index.

Now, we will have look about schema-less collections. Insert a totally different document into goodhealth, such as:
db.goodhealth.insert({name:"YashRim",gender:"m",home:"Allentown",worm: false})
use find to list the documents

Brief of Selectors
We will discuss about query selectors.
A MongoDB query selector is like the where clause of an SQL statement.

As such, you use it when finding, counting, updating and removing documents from collections. A selector is a JSON object, the simplest of which is {} which matches all documents (null works too). If we wanted to
find all female goodhealth, we could use {gender: "f"}.

Before delving too deeply into selectors, let’s set up some data to play with. First, remove what we”ve put so far in the goodhealth collection via: db.goodhealth.remove() (since we aren”t supplying a selector, it”ll remove all documents).

Sample insert documents, Documents means rows in a table(collection)

db.goodhealth.insert({name: "Rakesh", dob: new Date(1972,2,13,7,47), likes: ["carrot","papaya"],weight: 60, gender: "m", vampires: 63});

db.goodhealth.insert({name: "Lakshmirai", dob: new Date(1991, 0, 24, 13, 0), likes: ["carrot", "grape"], weight: 50, gender: "f", vampires: 43});

db.goodhealth.insert({name: "Sreeram", dob: new Date(1973, 1, 9, 22, 10), likes: ["energon","redbull"], weight: 84, gender: "m", vampires: 82});

db.goodhealth.insert({name: "Prathima", dob: new Date(1980, 7, 18, 18, 44), likes: ["apple"],weight: 55, gender: "f", vampires: 99});

db.goodhealth.insert({name: "Sarayu", dob: new Date(1985, 6, 4, 2, 1), likes:["apple", "carrot", "chocolate"], weight:55, gender:"f", vampires:80});

db.goodhealth.insert({name:"devika", dob: new Date(1998, 2, 7, 8, 30), likes: ["strawberry", "lemon" ], weight: 73, gender: "f", vampires: 40});

db.goodhealth.insert({name:"Durgapathi", dob: new Date(1979, 6, 1, 10, 42), likes: ["grape", "Rum"],weight: 70, gender: "m", vampires: 39});

db.goodhealth.insert({name: "Prabajan", dob: new Date(1978, 4, 3, 0, 57), likes: ["apple", "Beer"],weight: 84, gender: "m", vampires: 2});

db.goodhealth.insert({name: "sushma", dob: new Date(1979, 9, 8, 14, 53), likes: ["apple", "watermelon "], weight: 60, gender: "f", vampires: 33});

db.goodhealth.insert({name: "prakash", dob: new Date(1977, 2, 1, 5, 3), likes: ["apple", "watermelon"], weight: 78, gender: "m", vampires: 54});

db.goodhealth.insert({name: "sowji", dob: new Date(1999, 11, 20, 16, 15), likes: ["grape", "carrot"], weight: 54, gender: "f"});

db.goodhealth.insert({name: "jagadeesh", dob: new Date(1983, 6, 18, 18, 18), likes: ["grape", "watermelon"], weight: 70, gender: "m", vampires: 165});

Now we have some data, we can work with selectors. {field: value} is used to find any documents where field is equal to value. {field1: value1, field2: value2} is how we do an and statement.
The special $lt     :- Less than <
       $lte    :- Less than or equal <=
       $gt    :- Greater than >
       $gte    :- Greater than or equal >
       $ne     :- Not equal <>
For example, to get all male goodhealth that weight more than 70 Kgs, we could do:
db.goodhealth.find({gender: "m", weight: {$gt: 70}})
or quite the same thing, have a look
db.goodhealth.find({gender: {$ne: "f"}, weight: {$gte: 70}})
The $exists operator is used for matching the presence or absence of a field, for example:
db.goodhealth.find({vampires: {$exists: false}})
Should return a single document. If we want to OR rather than AND we use the $or operator and assign it to an array of values  
db.goodhealth.find({gender: "f", $or: [{likes: "apple"}, {likes: "orange"}, {weight: {$lt: 50}}]})

The above will return all female goodhealth which either likes apples or oranges or weight less than 50 Kgs.
Notice here, the likes field is an array. MongoDB supports arrays as first class objects. This is an incredibly handy feature. Once you start using it,

Have a look, selecting based on an array value is:
{likes: "watermelon"} will return any document where watermelon is a value of likes.

There are more available operators apart of this. The most flexible being $where which lets us supply JavaScript to execute on the server.

Some more advance queries will see on next article.

Popular Posts