For this simple test, all we need is the MongoClient class from the pymongo module. The example shown in this sub-section can be found at /path/to/repo/chapters/02/test_connection.py. Let's have a look at the code, as follows:
- We start by defining sample data and importing the needed classes, like this:
data = { 'first_name' : 'Fred', 'last_name' : 'Flintstone'}
import pprint
from pymongo import MongoClient
- Next, we create a MongoClient instance and a reference to the new database test, as follows:
client = MongoClient('localhost')
test_db = client.test
In order to maintain the purity of the test, as we are likely to run it many times, we add a statement that removes all documents from the new collection, test_collection.Â
- We then insert and retrieve a single document using the insert_one() and find_one()Â pymongo collection methods. A typical return value after an...