ComeçarComece de graça

Rounding up the G.S. crew

In chapter 2, you used a regular expression object Regex to find values that follow a pattern. We can also use the regular expression operator $regex for the same purpose. For example, the following query:

{ "name": {"$regex": "^Py"}    }

will fetch documents where the field 'name' starts with "Py". Here the caret symbol ^ means "starts with".

In this exercise, you will use regular expressions, projection, and list comprehension to collect the full names of laureates whose initials are "G.S.".

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.

# Find laureates whose first name starts with "G" and last name starts with "S"
docs = db.laureates.find(
       filter= {"firstname" : {"$____" : "^G"},
                  ____ : {____ : ____}  })
# Print the first document 
print(docs[0])
Editar e executar o código