ComeçarComece de graça

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'.

Este exercício faz parte do curso

Introduction to MongoDB in Python

Ver curso

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Connect to the "nobel" database
db = client.____
Editar e executar o código