Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Created September 19, 2015 07:12
Show Gist options
  • Save guyhughes/0ca861132fc85a08cd73 to your computer and use it in GitHub Desktop.
Save guyhughes/0ca861132fc85a08cd73 to your computer and use it in GitHub Desktop.
hackthenorth
#!/usr/bin/env python3
# python monogodb example from Hack The North 2015
import pymongo
conn = pymongo.MongoClient()
blog_db = conn["blog"]
posts_col = blog_db["posts"]
post = {
"title": "Learning MongoDB",
"tags": ["mongodb","tech"],
"content": "Blog post!",
"view": 0
}
posts_col.insert_one(post)
# postback happens here
print post["_id"]
# posts_col.update_one( query, update )
# .update_many...
query = { "$set":
{ "title": "MongoDB Rocks", "state": "published"}
}
query ={"$inc": {"views":1}} #increment views by 1
# solves race condition
#array ops
query = { "$push" :
{ "tags": "webscale"}
}
query = { "$addtoset": {"tags": "webscale"}}
# if and only if not there, add to a set
"$pull"
# posts_col.delete_one(query)
# bit.ly/MongoDBHackTheNorth
# [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment