Skip to content

Instantly share code, notes, and snippets.

@kenwalger
Created January 23, 2017 00:09
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 kenwalger/d0ebd3ec962538bc59a8b65a7ac6d585 to your computer and use it in GitHub Desktop.
Save kenwalger/d0ebd3ec962538bc59a8b65a7ac6d585 to your computer and use it in GitHub Desktop.
import pprint
from pymongo import MongoClient
__author__ = 'Ken W. Alger'
HOST = 'localhost'
PORT = 27017
client = MongoClient(HOST, PORT)
database = client.cookbook
recipes = database.recipes
# Create Document
recipe = {'title': 'chocolate milk',
'description': 'Yummy drink',
'ingredients': [
{'name': 'milk', 'quantity': 8, 'unit of measure': 'ounce'},
{'name': 'chocolate syrup', 'quantity': 2, 'unit of measure':
'ounce'}
],
'yield': {'quantity': 1, 'unit': 'glass'},
'prep time': 0,
'cook time': 0,
'author': 'Biff Tannen',
'uploaded_by': 'kenwalger',
}
recipes.insert_one(recipe)
# Read Document
print("\nPretty Print: \n")
pprint.pprint(recipes.find_one())
# Update Document
recipes.update_one({'title': 'chocolate milk'},
{'$set': {'author': 'George McFly'}
}
)
print("\nShould be George McFly: ")
pprint.pprint(recipes.find_one({'author': 'George McFly'}))
print("\nShould not be found")
pprint.pprint(recipes.find_one({'author': 'Biff Tannen'}))
# Delete Document
recipes.delete_one({'author': 'George McFly'})
print('Deleted the record:')
pprint.pprint(recipes.find_one())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment