Mongodb is an opensource document database in which data are stored as documents.That is,a database includes many collections and collections includes many documents.A document is field and value pairs.Value may be other document,arrays,or arrays of documents.For a python programmer,document may be considered as as a dictionary and collection can be considered as a list of dicts.
example of a document:
{"author": "Mike","text": "My first blog post!"}
Here i am defining how to connect mongo db using Python program.This might help in Flask framework.
First you need to install pymongo
sudo pip install pymongo
In flask route program,
import pymongo
from pymongo import MongoClient
Then create mongo instance,
client=MongoClient() # here in flask,it comes below of app=Flask(__name__)
db=client.testdatabase # testdatabase is the database in mongodb
to connect with a collection,
collection=db.testcollection # testcollection is a collection in testdatabase.
collection.insert(data)
for a specific document(filtering),
collection.find({'author':'Mike'})
Operations can be done as what doing with list of dict in python.for eg:
for i in collection.find():
print i
This gives each documents(dict in python) in the collection(list in python)
I designed a Paint App in Flask with Mongodb as database using a save button.
To see the code Click here
example of a document:
{"author": "Mike","text": "My first blog post!"}
Here i am defining how to connect mongo db using Python program.This might help in Flask framework.
First you need to install pymongo
sudo pip install pymongo
In flask route program,
import pymongo
from pymongo import MongoClient
Then create mongo instance,
client=MongoClient() # here in flask,it comes below of app=Flask(__name__)
db=client.testdatabase # testdatabase is the database in mongodb
to connect with a collection,
collection=db.testcollection # testcollection is a collection in testdatabase.
Insertion of data:
data={"author": "Mike","text": "My first blog post!"}collection.insert(data)
Remove of data:
collection.remove()retrieve of data:
collection.find()for a specific document(filtering),
collection.find({'author':'Mike'})
Operations can be done as what doing with list of dict in python.for eg:
for i in collection.find():
print i
This gives each documents(dict in python) in the collection(list in python)
I designed a Paint App in Flask with Mongodb as database using a save button.
To see the code Click here
No comments:
Post a Comment