Skip to content

Instantly share code, notes, and snippets.

@Phrancis
Last active September 10, 2015 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phrancis/00fd27350ebded710349 to your computer and use it in GitHub Desktop.
Save Phrancis/00fd27350ebded710349 to your computer and use it in GitHub Desktop.
MongoDb fun with PyMongo
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