Last active
February 12, 2019 13:42
-
-
Save fmasanori/5922895 to your computer and use it in GitHub Desktop.
CRUD MongoDB exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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