MongoEngine – Query Operators

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
from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
qset = Books.objects
for book in qset.filter(book_price__gt=500):
print('ID:', book.book_id, 'Name:',
book.book_name, 'Price:', book.book_price)

Output

ID: 1003 Name: Machine Learning Price: 1000.00
ID: 1004 Name: Data Structure Price: 1300.00

The in operator is like Python’s in operator. For name of product matching with names in list, the following code is used −

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
qset = Books.objects
for book in qset.filter(book_price__in=range(500, 2000)):
print('ID:', book.book_id, 'Name:',
book.book_name, 'Price:', book.book_price)

Output

ID: 1003 Name: Machine Learning Price: 1000.00
ID: 1004 Name: Data Structure Price: 1300.00

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 book details for name containing ‘data’ in name −

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
qset = Books.objects
for book in qset.filter(book_name__icontains='data'):
print('ID:', book.book_id, 'Name:',
book.book_name, 'Price:', book.book_price)

Output

ID: 1004 Name: Data Structure Price: 1300.00
ID: 1005 Name: Data Structure & Algorithm Price: 1240.70
ID: 1006 Name: Data Science Price: 1600.00

In another example of string query, the following code displays name ending with ‘hm’−

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
qset = Books.objects
for book in qset.filter(book_name__endswith='hm'):
print('ID:', book.book_id, 'Name:',
book.book_name, 'Price:', book.book_price)

Output

ID: 1005 Name: Data Structure & Algorithm Price: 1240.70
ID: 1007 Name: Data Analysis and Algorithm Price: 1680.00
MongoEngine – Filters (Prev Lesson)
(Next Lesson) MongoEngine – Indexes
', { 'anonymize_ip': true });