How to Use Pymongo to Store Data

Version 8.1 by Leon Poon on 2021/03/23 05:17

First, set up a virtualenv.

Make sure that your mongo server is running.

In the virtualenv, install pymongo, then try out this .py file in the virtualenv.

from pymongo import MongoClient
client = MongoClient()
db = client['test-database']
collection = db['test-collection']
print(collection.insert_one({'my_key': 'some value'}))

The printout shows the ID assigned by mongodb to the data you stored. You can now try to look it up.

You can also follow tutorial.