Last active
September 10, 2015 01:29
-
-
Save Phrancis/00fd27350ebded710349 to your computer and use it in GitHub Desktop.
MongoDb fun with PyMongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
phrancis = { | |
"userName": "Phrancis", | |
"firstName": "Francis", | |
"lastName": "Gaboury", | |
"roles": ["Game Designer", "Web Designer"], | |
"countryOfOrigin": "Canada", | |
"likes": ["spicy foods", "racing games", "databases"] | |
} | |
zomis = { | |
"userName": "Zomis", | |
"firstName": "Simon", | |
"lastName": "Forsberg", | |
"roles": ["Java Developer", "Game Developer"], | |
"countryOfOrigin": "Sweden", | |
"likes": ["bubbles", "java", "clean code"] | |
} | |
with pymongo.MongoClient() as conn: | |
db = conn[u'local'] | |
print db | |
collection = db.my_collection | |
print "Collection: {}".format(collection) | |
# clear collection | |
collection.remove() | |
collection.insert(phrancis) | |
collection.insert(zomis) | |
for user in collection.find({"userName": "Zomis"}): | |
print user | |
for user in collection.find({"roles": "developer"}): | |
print user | |
# {u'userName': u'Zomis', u'firstName': u'Simon', u'roles': [u'Java Developer', u'Game Developer'], u'countryOfOrigin': u'Sweden', u'lastName': u'Forsberg', u'likes': [u'bubbles', u'java', u'clean code'], u'_id': ObjectId('55f0d343e9e1722e3871942b')} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment