Monday, March 25, 2013

MongoDB

           Now Let's going through Simple NoSQL database MongoDB. MongoDB is leading NoSQL database,designed for how we build and run application today. NoSQL is designed for distributed data stores where very large scale of data storing is needed. It is a database system that provides simple lightweight mechanism for storage and retrieval of data.NoSQL has become much popular in the last few years.MongoDB is a general purpose, open-source NoSQL database.
Features of MongoDB:
  • Document data model with dynamic schemas
  • Full, flexible index support and rich queries
  • Auto-Sharding for horizontal scalability
  • Built-in replication for high availability
  • Text search
  • Advanced security
  • Aggregation Framework and MapReduce
  • Large media storage with GridFS
Organizations of all sizes use MongoDB to quickly and easily develop, scale and operate applications. Instead of storing data in rows and columns as one would with a relational database, MongoDB stores a binary form of JSON documents (BSON).

 MongoDB Installation:
      Now let's see how to get started  with MongoDB.This tutorial provide basic installation steps for installing MongoDB on Ubuntu.12.04 Linux system, where we uses  .deb package as the basis of the installation.
Enter the following command to import the 10gen public GPG Key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.list file and include the following line for the 10gen repository.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Now Enter the following command to reload your repository:
sudo apt-get update
Issue the following command to install MongoDB:
sudo apt-get install mongodb-10gen
When this command completes, you have successfully installed MongoDB!
You can start the mongod process by issuing the following command:
sudo service mongodb start
As needed, you may stop the mongod process by issuing the following command:
sudo service mongodb stop
You can connect to your MongoDB instance by issuing the following command at the system prompt:
mongo
This will connect to the database running on the localhost interface by default. For more information please refer the link:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/


pymongo

pymongo is python package index. We prefer pip to install pymongo on python:
pip install pymongo
In Python shell, type the following command to import pymongo package.
import pymongo
To connect to the database, we create a database instance- a connection object by including the following in our python shell.
from pymongo import Connection
connection = Connection()
collection = connection.DB_NAME.COLLECTION_NAME
now we are ready to insert database.
collection.insert({"fruit": "Mango", "color": "Orange"})
The above code snippet is enough for inserting a row(document or BSON document in terms of MongoDB) in the table(collection) collection_name.
For retrieving all the data entered into the collection collection_name,
collection.find()
It's also possible to get the specific item in the collection.
collection.find_one({"color": "Orange",})
Reading all the documents of the specified kind, is by,
collection.find({"color": "Orange", })

for more details please click Here.


for sql to mongoDB convertion also refer the link http://docs.mongodb.org/manual/reference/sql-comparison/


Paint application with MongoDB

Now, I have replaced database of My Paint Application from Sqlite3 to MongoDB. The html front end and javascripts are same.Please refer my earlier post about paint application for more info.I will explain how to connect to database and retrive data from it.


from flask import Flask, render_template, request, redirect
from pymongo import Connection

app = Flask(__name__)


connection = Connection()

collection = connection.paintdb.drawings

@app.route('/', methods=['GET', 'POST'])

def paint():
    if request.method == 'GET':
        all_data = {}
        datas = collection.find()
        for data in datas:
            all_data[data["fname"]] = data["image_data"]
        return render_template('paint.html', py_all= all_data)
    elif request.method == 'POST':
        filename = request.form['fname']
        data = request.form['whole_data']
        collection.insert({"fname":filename, "image_data":data})
        return redirect('/')

if __name__ == '__main__':

   app.run()
from pymango module import class "Connection".It is used to connect with database.Then form a collection(table),named as drawings. Datas are inserted directly into collection. 

The complete code is Here

No comments:

Post a Comment