Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active February 12, 2019 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fmasanori/5922895 to your computer and use it in GitHub Desktop.
Save fmasanori/5922895 to your computer and use it in GitHub Desktop.
CRUD MongoDB exception
import pymongo
import sys
def main():
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.m101
people = db.people
person = {'name': 'Barack Obama', 'role':'President',
'address':{'address1':'The White House',
'street': '1600 Pensylvania Avenue',
'state': 'DC',
'city': 'Washington'},
'interests':['government', 'basketball', 'the middle east']}
people.insert_one(person)
person = people.find_one({'name': 'Barack Obama'})
print (person)
try:
people.insert_one(person)
except:
print ('insertion failed', sys.exc_info()[0])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment