Skip to content

Instantly share code, notes, and snippets.

@skatesham
Forked from fmasanori/CRUD MongoDB.py
Created August 8, 2017 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skatesham/575a0d45f831e2d48b7bc07633b62e3b to your computer and use it in GitHub Desktop.
Save skatesham/575a0d45f831e2d48b7bc07633b62e3b to your computer and use it in GitHub Desktop.
CRUD MongoDB Python2
from datetime import datetime
from pymongo import MongoClient
connection = MongoClient("mongodb://localhost")
db = connection.test
post = {"title": "My Blog Post",
"content": "Here's my blog post.",
"date": datetime.utcnow()}
db.blog.insert(post)
print "find retorna um cursor"
cursor = db.blog.find()
for d in cursor:
print d
print "find_one retorna um documento"
d = db.blog.find_one()
print d
db.blog.remove({"title": "My Blog Post"})
print "depois de remover o post"
d = db.blog.find_one()
print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment