Skip to content

Instantly share code, notes, and snippets.

@jintao-zero
Created August 18, 2017 09:06
Show Gist options
  • Save jintao-zero/dee8d8db211f4b1c33abb3722a6932dc to your computer and use it in GitHub Desktop.
Save jintao-zero/dee8d8db211f4b1c33abb3722a6932dc to your computer and use it in GitHub Desktop.
python mongo test
#!/usr/bin/env python
import pymongo
import pprint
print 'this is a python test'
mongo_host = 'localhost'
#http://api.mongodb.com/python/current/tutorial.html#getting-a-database
def test_mongo():
client = pymongo.MongoClient()
#db = client.test_database
db = client['test_database']
if db is None:
print 'db None'
else:
print 'db not None'
#col = db.test_collection
col = db['test_collection']
post_id = col.insert_one({'key': 'v'}).inserted_id
cursor = col.find({'key':'v'})
for post in cursor:
pprint.pprint(post)
print (post['key'])
#col.drop()
if __name__ == '__main__':
test_mongo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment