Search This Blog

Thursday, 29 May 2014

Talking to MongoDB

In our previous post we saw how to set up MongoDB for running. The application we started was the MongoDB server. It is not a client. To perform any operations, what we need is a way to connect to the server. For this I decided to initially use the mongo shell:

The mongo comand will fire up the shell. Within the shell, the "show dbs" command indicates I have a database named local. Remember the local.0 file from our data folder.
The "db" command indicates the database you are connected to. I would have exepected to see the name local but this shows test.(Kind of matches the initial connect action performed by mongo shell)
The first step would be to create and use my own database.
 
I used the "use" command to create the new database named fruits.According to the MongoDB docs,
MongoDB will not permanently create a database until you insert data 
into that database.
The next step was to add a document into our database. No tables, no schemas. We simply add our record or more correctly our document.
I created a JSON object that holds the properties of the apple fruit. Like Java script code, I simply assigned this object to a variable. To add this object to the database we used the insert method.
So what about collections ? I had read that documents are added to collections. But the insert method used seemed to be associated with the database.
On checking the docs:
MongoDB will create a collection implicitly upon its first use. You do not need 
to create a collection before inserting data. Furthermore, because MongoDB uses 
dynamic schemas, you also need not specify the structure of your documents before 
inserting them into the collection
Here db.fruits.insert method does not refer to the database fruits. Instead it refers to a new collection named fruits that will be created within the fruits database. The json will also be added to this collection. So now we have a json document for apple in the fruits collection within the fruits database.
What exactly is a dynamic schema ?
MongoDB uses dynamic schemas. You can create collections without defining 
the structure, i.e. the fields or the types of their values, of the documents 
in the collection. You can change the structure of documents simply by adding 
new fields or deleting existing ones. Documents in a collection need not have 
an identical set of fields - MongoDB Docs
The relational databases- Oracle,MySql, Postgres... they all have schemas. The structure of their data is defined before any data is created.I remember studying Codds rules in college - which focused on the structure of data. On the importance of every record in a table following the same format. (For a flashback check this link) This schema looses its prime posistion in MongoDB. So these records can also be inserted in fruits:
As seen above my fruit collection includes an apple document, a ship document and even a person document. Everything is fine
Another point of note is the _id field. This is the primary key for the document. It is an auto generated value added by MongoDB. That does not mean we could not provide our own.
As seen here we directly added the json object to our new collection. Like passing an anonymous class object to a method in java.
If we look at the records we can see that no id was added. The _id value that we provided was used. Only care needs to be taken that the value is unique.

No comments:

Post a Comment