Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active September 1, 2020 14:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fmasanori/5903005 to your computer and use it in GitHub Desktop.
Save fmasanori/5903005 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_one(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.delete_one({"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