MongoEngine Tutorial
Selected Reading
- MongoEngine - Discussion
- MongoEngine - Useful Resources
- MongoEngine - Quick Guide
- MongoEngine - Extensions
- MongoEngine - Text Search
- MongoEngine - Signals
- MongoEngine - GridFS
- MongoEngine - Javascript
- MongoEngine - Atomic Updates
- MongoEngine - Document Inheritance
- MongoEngine - Advanced Queries
- MongoEngine - Aggregation
- MongoEngine - Indexes
- MongoEngine - Custom Query Sets
- MongoEngine - Sorting
- MongoEngine - QuerySet Methods
- MongoEngine - Query Operators
- MongoEngine - Filters
- MongoEngine - Querying Database
- MongoEngine - Add/Delete Document
- MongoEngine - Fields
- MongoEngine - Dynamic Schema
- MongoEngine - Document Class
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Installation
- MongoEngine - Object Document Mapper
- MongoEngine - MongoDB Compass
- MongoEngine - MongoDB
- MongoEngine - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MongoEngine - Query Operators
MongoEngine - Query Operators
In addition to = operator to check equapty, 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 pst |
nin | value is not in pst |
mod | value % x == y, where x and y are two provided values |
all | every item in pst 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 pke Python’s in operator. For name of product matching with names in pst, 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: 12500Advertisements