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.".
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.
# 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])