List fields of a document
The .find_one()
method of a collection can be used to retrieve a single document. This method accepts an optional filter
argument that specifies the pattern that the document must match. You will learn more about filters in the next lesson, but for now, you can specify no filter or an empty document filter ({}
), in which case MongoDB will return the document that is first in the internal order of the collection.
This method is useful when you want to learn the structure of documents in the collection.
In Python, the returned document takes the form of a dictionary:
sample_doc = {'id' : 12345, 'name':'Donny Winston', 'instructor': True}
The keys of the dictionary are the (root-level) "fields" of the document, e.g. 'id'
, 'name'
,'instructor'
.
This exercise is part of the course
Introduction to MongoDB in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Connect to the "nobel" database
db = client.____