MongoDB

From XPUB & Lens-Based wiki
Revision as of 10:13, 31 May 2012 by Timo (talk | contribs) (Created page with "Simple Database connect, insert and find <source lang='python'> import pymongo from pymongo import Connection import datetime connection = Connection() myDB = connection['timo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Simple Database connect, insert and find

import pymongo
from pymongo import Connection
import datetime

connection = Connection()
myDB = connection['timo-database']
##WE ALL SHARE THE SAME MONGO INSTNACE SO USE UNIQUE NAMES FOR NOW


post1 = {"author": "Mike", 'age':21,"text": "My first blog post!","tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow() }

post2 = {"author": "Timo", 'age':28,"text": "Timos first blog post!","tags": ["db", "python", "pymongo"], "date": datetime.datetime.utcnow() }

post3 = {"author": "Someone", 'age':30, 'place':'Rotterdam'}

collection = myDB.collection

collection.insert(post2)
collection.insert(post1)
collection.insert(post3)

#cursor = myDB.collection.find();
#print cursor[0]

print myDB.collection.find_one({'place':"Rotterdam"})['age']

myDB.collection.update( {'author':'Timo'} , {'$set': {'age':29} } )

print myDB.collection.find_one({'author':"Timo"})