- MongoEngine - Home
- MongoEngine - MongoDB
- MongoEngine - MongoDB Compass
- MongoEngine - Object Document Mapper
- MongoEngine - Installation
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Document Class
- MongoEngine - Dynamic Schema
- MongoEngine - Fields
- MongoEngine - Add/Delete Document
- MongoEngine - Querying Database
- MongoEngine - Filters
- MongoEngine - Query Operators
- MongoEngine - QuerySet Methods
- MongoEngine - Sorting
- MongoEngine - Custom Query Sets
- MongoEngine - Indexes
- MongoEngine - Aggregation
- MongoEngine - Advanced Queries
- MongoEngine - Document Inheritance
- MongoEngine - Atomic Updates
- MongoEngine - Javascript
- MongoEngine - GridFS
- MongoEngine - Signals
- MongoEngine - Text Search
- MongoEngine - Extensions
- MongoEngine Useful Resources
- MongoEngine - Quick Guide
- MongoEngine - Useful Resources
- MongoEngine - Discussion
MongoEngine - Query Operators
In addition to = operator to check equality, the following logical operators are defined in MongoEngine.
| ne | not equal to |
| lt | less than |
| lte | less than or equal to |
| gt | greater than |
| gte | greater than or equal to |
| not | negate a standard check, may be used before other operators |
| in | value is in list |
| nin | value is not in list |
| mod | value % x == y, where x and y are two provided values |
| all | every item in list of values provided is in array |
| size | the size of the array is |
| exists | value for field exists |
These operators must be attached to field name with double underscore __.
To use greater than (gt) operator, use the following format −
#using greater than operator
for product in products.objects(price__gt=10000):
print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)
Output
ID: 1 Name: Laptop Price: 25000 ID: 2 Name: TV Price: 50000 ID: 5 Name: Printer Price: 12500
The in operator is like Pythons in operator. For name of product matching with names in list, the following code is used −
for product in products.objects(Name__in=['TV', 'Printer']):
print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)
Output
ID: 2 Name: TV Price: 50000 ID: 5 Name: Printer Price: 12500
You can use following operators as shortcut for regex expressions for applying filter to queries −
| exact | string field exactly matches value |
| iexact | string field exactly matches value (case insensitive) |
| contains | string field contains value |
| icontains | string field contains value (case insensitive) |
| startswith | string field starts with value |
| istartswith | string field starts with value (case insensitive) |
| endswith | string field ends with value |
| iendswith | string field ends with value (case insensitive) |
| match | performs an $elemMatch so you can match an entire document within an array |
For example, the following code prints product details for name containing o in name −
for product in products.objects(Name__contains='o'):
print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)
Output
ID: 1 Name: Laptop Price: 25000 ID: 3 Name: Router Price: 2000
In another example of string query, the following code displays name ending with er−
for product in products.objects(Name__endswith='er'):
print ('ID:',product.ProductID, 'Name:',product.Name, 'Price:',product.price)
Output
ID: 3 Name: Router Price: 2000 ID: 4 Name: Scanner Price: 5000 ID: 5 Name: Printer Price: 12500
Advertisements