Search This Blog

Showing posts with label nosql. Show all posts
Showing posts with label nosql. Show all posts

Wednesday, December 25, 2013

Quick summary of RDMS and NoSQL MongoDB

Regardless is this is RDMS or NoSQL data base engine the very basic purpose of these systems is to allow data collection and modification. This can be visualized :



References

http://en.wikipedia.org/wiki/NoSQL

Thursday, December 19, 2013

Exercise 2 and 3 - the order of functions

Important note: This article is in relation to online MongoDB course. For more information about the course and other posts describing its content please check my main page here: M101P: MongoDB for Developers course. 

Importance and order of functions in MongoDB when retrieving data

When running week 1 homework scripts we get following results:


The code is simple. There is only one line of code that is interesting.

iter = collection.find({},limit=1, skip=n).sort('value', direction=1)

There are two methods executed one after another. The first one is find and the second sort. At fist it looks like the sort method is only applied to the results returned by find but this is not truth.

What is actually happening is:
  • The find method returns all documents from the data base (the order of the values is determined by the database what can be read as random; this would be the equivalent of a select * from table statement in SQL)
  • The list is then sorted base on the value. The value represent here one of the document attributes.
  • After the returned documents (i.e. returned rows from table in RDMS data base) are sorted we skip the first 'n' rows and return a single element.

My M101P: MongoDB for Developers course

Below are some of my notes from the MongoDB course i did. This list will grow as the course progresses.

Online MongoDb Course

The course video can be found here:
https://education.mongodb.com/courses

Source code and further example code
https://github.com/rtomaszewski/mongo-course

A quick function summary of the mongo shell and API
http://www.hodgin.ca/downloads/Mongo%20Shell%20Cheat%20Sheet.pdf

Shell documentation
http://docs.mongodb.org/manual/reference/method/
http://docs.mongodb.org/manual/reference/operator/

Pymongo docs
http://api.mongodb.org/python/current/
https://pypi.python.org/pypi/pymongo/

Week #1
Week #2

Connect pymongo to bottle framework

Important note: This article is in relation to online MongoDB course. For more information about the course and other posts describing its content please check my main page here: M101P: MongoDB for Developers course. 

Install software
Testing

References

http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find

Sunday, November 3, 2013

Simple MongoDB tutorial

MongoDB has become one of the popular NoSQL db engines. It is considered as a NoSQL db and uses some concepts and names that are different from these used in relational database systems.

Alongside MongoDB there are many others NoSQL dbs. An short summary of available db options and theirs features can be found here.

Below are some of my notes how to start with MongoDB.

How to install MongoDB
How to create your first table in the NoSQL data base

It is important to note that data bases will be created dynamically after you insert a first data into a collection (e.i insert a row into your table in the db - collection is a name for table in mongo).

http://docs.mongodb.org/manual/reference/sql-comparison/
http://docs.mongodb.org/manual/tutorial/getting-started/
http://www.mkyong.com/mongodb/how-to-create-database-or-collection-in-mongodb/

How to insert data into table

http://docs.mongodb.org/manual/tutorial/getting-started/

# start the shell interpreter
$ mongo
> show dbs
help    (empty)
local   0.078125GB
test    (empty)

> use rado
switched to db rado

> j = { name : "hello  word" }
{ "name" : "hello  word" }
> k = { x : 3 }
{ "x" : 3 }

> db.mycollection1.insert(j)
> db.mycollection1.insert(k)

> show dbs
help    (empty)
local   0.078125GB
rado    0.203125GB  <<<
test    (empty)

> use rado
> show collections
mycollection1  <<<<< 
system.indexes

> db.mycollection1.find()
{ "_id" : ObjectId("52767264795748b715336b87"), "x" : 3 }
{ "_id" : ObjectId("5276726a795748b715336b88"), "name" : "hello  word" }

Mongo shell commands 

http://docs.mongodb.org/manual/reference/method/

Install python API library for MongoDB
  • Install the library 
http://docs.mongodb.org/manual/applications/drivers/
http://docs.mongodb.org/ecosystem/drivers/python/

apt-get install python-pymongo
  • Customize the bash environment and install ipython 
http://rtomaszewski.blogspot.co.uk/2013/10/home-directory-and-dotfiles-management.html

 apt-get install ipython

Your first Python script that connects to MongoDB 

http://api.mongodb.org/python/current/tutorial.html
http://docs.mongodb.org/manual/reference/method/db.collection.insert/

Example Python code to insert data to our Mongo collection:
 
# rado-hello-world.py

import pymongo
from pymongo import MongoClien

client = MongoClient()
db = client['rado']

db.collection_names()
[u'system.indexes', u'mycollection1']

collection = db['mycollection1']

for doc in collection.find():
   ....:     print doc
   ....:
{u'x': 3.0, u'_id': ObjectId('52767264795748b715336b87')}
{u'_id': ObjectId('5276726a795748b715336b88'), u'name': u'hello  word'}

l = { "from" : "python", "mytext" : "hello from python" }
collection.insert(l)
ObjectId('52767d071d011c22eb1a7de5')

for doc in collection.find():
    print doc
   ....:
{u'x': 3.0, u'_id': ObjectId('52767264795748b715336b87')}
{u'_id': ObjectId('5276726a795748b715336b88'), u'name': u'hello  word'}
{u'_id': ObjectId('52767d071d011c22eb1a7de5'), u'from': u'python', u'mytext': u'hello from python'}

Further info about write operations 

Once of the big differences when working with NoSQL is that they are an implement of CRUD instead of ACID operation.

http://rsmith.co/opinion/technology/2012/11/05/mongodb-gotchas-and-how-to-avoid-them/
http://docs.mongodb.org/manual/core/write-operations/
http://docs.mongodb.org/manual/core/write-concern/
http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#module-pymongo.mongo_client

We could say than that the performance increase is at the cost of security and integrity. If you don't want to lost any data and see errors as you type the commands you can use the options below when connecting to mongo DB. Please keep in mind that this trade off will have some performance implication in busy production systems although
 
# default 
>>> client = MongoClient()
>>> client2 = MongoClient( j=True, fsync=True )

>>> client2.write_concern
 {'fsync': True, 'j': True}

>>> client.write_concern
 {}