Python Tutorials · Python MongoDB

MongoDB Query

Learn all about MongoDB Query in this comprehensive tutorial.

5 min read advanced
  • When finding documents in a collection, you can filter the result by using a query object.
  • To make advanced queries you can use modifiers as values in the query object.
  • You can also use regular expressions as a modifier.

Filter the Result

When finding documents in a collection, you can filter the result by using a query object.

The first argument of the find() method is a query object, and is used to limit the search.

python

Advanced Query

To make advanced queries you can use modifiers as values in the query object.

E.g. to find the documents where the "address" field starts with the letter "S" or higher (alphabetically), use the greater than modifier: {"$gt": "S"}:

python

Filter With Regular Expressions

You can also use regular expressions as a modifier.

Note: Regular expressions can only be used to query strings.

To find only the documents where the "address" field starts with the letter "S", use the regular expression {"$regex": "^S"}:

python

Module quiz

2 questions
1

Which of the following is true about MongoDB Query?

2

What is the most common pitfall when working with MongoDB Query?

Answer all questions to submit.