Skip to content

Instantly share code, notes, and snippets.

@TutorialDoctor
Created December 11, 2018 21:13
Show Gist options
  • Save TutorialDoctor/bf9778ba7b8c01c661706bf743800151 to your computer and use it in GitHub Desktop.
Save TutorialDoctor/bf9778ba7b8c01c661706bf743800151 to your computer and use it in GitHub Desktop.
Testing pymongo for python with a mongo database
from pymongo import MongoClient
#pip3 install pymongo
if __name__ == "__main__":
client = MongoClient()
db = client.test_database
people = db.people
people.insert_one({'name':'Mike','food':'cheese'})
people.insert_one({'name':'John','food':'ham','location':'UK'})
people.insert_one({'name':'michelle','food':'cheese'})
print()
print("INSERT 7 FIND TEST")
peeps = people.find()
for person in peeps:
print(person)
print('\n')
print("FIND WITH DICT")
peeps = people.find({'food':'cheese'})
for person in peeps:
print(person)
print('\n')
print("REGEX TEST")
peeps = people.find({'name':{'$regex':'*[Mn]i.*'}})
for person in peeps:
print(person)
# person = people.find_one({'food':'ham'})
# person['food'] = 'eggs'
# people.replace_one(person) #person.save() is deprecated
# person.replace_one() or person.insert_one()
print('UPDATE RECORD TEST')
for person in people.find({'food':'eggs'}):
print(person)
for person in people.find():
people.remove(person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment