Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Sunday, January 11, 2015

How to retrieve data from MongoDB and display via web applications using Python Bottle Framework

First start the Mongo server using mongod command. In this example the following document data base has been used.

Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

  • Routing: Requests to function-call mapping with support for clean and dynamic URLs.
  • Templates: Fast and pythonic built-in template engine and support for mako, jinja2and cheetah templates.
  • Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
  • Server: Built-in HTTP development server and support for paste, fapws3, bjoern,gae, cherrypy or any other WSGI capable HTTP server.
Run this Code and run the web browser http://localhost:8080/
************************************************************
Code:
************************************************************
import bottle
import pymongo


@bottle.route('/')
def home_page():


   #connect to mongodb
   connection = pymongo.MongoClient('localhost', 27017)
   #connect to mydb database
   db = connection.mydb
   #handle to friends Collection
   data = db.friends
   #getting all the document from friends
   friendsList = data.find()
   #construct string variable with friends names
   myfriendnames = ""
   for item in friendsList:
       myfriendnames = myfriendnames + ", " + item["name"]


   return myfriendnames


bottle.debug(True)

bottle.run(host='localhost', port=8080)

Cheers!
Uma

Saturday, January 10, 2015

How to retrieve data from MongoDB using Python

First PyMongo module should install according to the Python version that you are going use. You can find the PyMongo in the following link:
Then, create a database and collection on MongoDB with database name: mydb and collection Name: friends as shown below.
The following code will retrieve the data as shown below.
Code:
*******************************************************************
import pymongo
from pymongo import MongoClient


#connect to Database
connection = MongoClient('localhost', 27017)
db = connection.mydb


#handle to friends Collection
data = db.friends


friendsList = data.find()


for item in friendsList:

   print("Name: " + item["name"] + "Age: " + str(item["age"]))
******************************************************************

Cheers!
Uma

Sunday, November 2, 2014

MongoDB Overview

https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRPQlQur8o_5jMo6lLJlr7-NQg7DYkzNvhrPwy7wY-xRIjjGSt-
MongoDB is the most popular NoSQL database system. The word MongoDB comes from ‘humongous’. It is a cross-platform document-oriented database system. It is free and open source software. It developed by Software Company 10gen in October 2007. It shifted to open source development model in 2009. MongoDB is written in C++ language and supports with dynamic schemas. MongoDB calls the format BSON.
MongoDB has been adopted as backend software by a number of major websites and services.
The following image show the operational database landscape


MongoDB was designed from the ground up to be easy scale to multiple distributed servers. Two of the biggest problems in distributed database design are distributed join operation and distributed transactions. Both of these operations are complex to implement, and can yield poor performance or even downtime in the event the server become unreachable. 

By kicking on these problems and not supporting joins or multi document transactions at all, MongoDB has been able to implement an automatic sharding solution with much better scaling and performance characteristics than you’d normally be stuck with if you had to take relational joins and transactions into account.
The following images shows the relation between relational data and BSON file format
MongoDB is built for agile development. It has schema less and this is dynamic schema, which can evolve as applications evolve without requiring expensive migrations. In addition, MongoDB stores data using JSON-like documents, which map naturally to object-oriented programming. The documents stored in the database can have varying sets of fields, with different types for each field. One could have the following objects in a single collection:
{ name : “Joe”, x : 3.3, y : [1,2,3] } { name : “Kate”, x : “abc” } { q : 456 }

Cheers!
Uma