Skip to content

Query Operators

Creating queries in PyODMongo is straightforward and intuitive. It simplifies the process of building MongoDB queries, providing a Pythonic and straightforward approach.

In PyODMongo, a query serves as an essential attribute of the find_many, find_one, delete and save methods, which are available through the DbEngine and AsyncDbEngine classes. These methods empower you to retrieve data from your MongoDB database with ease, combining the simplicity of Python with the robust querying capabilities of MongoDB.

Operators

Equal

The Equal operator is used to match documents where the value of a field is equal to the specified value. It's a basic comparison operator in MongoDB and PyODMongo.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.name == "Box"
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import eq

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = eq(Product.name, "Box")
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{name: {$eq: "Box"}}

Grater than

The Greater than operator is used to match documents where the value of a field is greater than the specified value. It helps in filtering out records that exceed a certain threshold.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.price > 10
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import gt

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = gt(Product.price, 10)
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{price: {$gt: 10}}

Grater than equal

The Greater than equal operator matches documents where the value of a field is greater than or equal to the specified value. This is useful for range queries including the boundary value.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.price >= 10
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import gte

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = gte(Product.price, 10)
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{price: {$gte: 10}}

In

The In operator allows you to specify an array of possible values for a field. It matches documents where the field’s value is in the specified array.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import in_

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = in_(Product.name, ["Ball", "Box", "Toy"])
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{name: {$in: ["Ball", "Box", "Toy"]}}

Lower than

The Lower than operator matches documents where the value of a field is less than the specified value. This is used for filtering records below a certain threshold.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.price < 10
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import lt

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = lt(Product.price, 10)
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{price: {$lt: 10}}

Lower than equal

The Lower than equal operator matches documents where the value of a field is less than or equal to the specified value. This includes the boundary value in the results.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.price <= 10
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import lte

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = lte(Product.price, 10)
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{price: {$lte: 10}}

Not equal

The Not equal operator matches documents where the value of a field is not equal to the specified value. It is used to exclude documents with a specific value.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.name != "Box"
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import ne

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = ne(Product.name, "Box")
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{name: {$ne: "Box"}}

Not in

The Not in operator allows you to specify an array of values that the field should not be equal to. It matches documents where the field’s value is not in the specified array.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import nin

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = nin(Product.name, ["Ball", "Box", "Toy"])
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{name: {$nin: ["Ball", "Box", "Toy"]}}

And

The And operator is used to join multiple query clauses with a logical AND. It ensures that all specified conditions must be true for a document to be included in the result set.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = (Product.price > 10) & (Product.price <= 50)
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import and_, gt, lte

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = and_(gt(Product.price, 10), lte(Product.price, 50))
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{$and: [{price: {$gt: 10}}, {price: {$lte: 50}}]}

Or

The Or operator joins query clauses with a logical OR, matching documents that satisfy at least one of the specified conditions.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = (Product.name == "Box") | (Product.price == 100)
box: Product = engine.find_one(Model=Product, query=query)
from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import or_, eq

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = or_(eq(Product.name, "Box"), eq(Product.price, 100))
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{$or: [{name: {$eq: "Box"}}, {price: {$eq: 100}}]}

Nor

The Nor operator joins query clauses with a logical NOR, matching documents that fail all of the specified conditions.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import nor, eq

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = nor(eq(Product.name, "Box"), eq(Product.price, 100))
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{$nor: [{name: {$eq: "Box"}}, {price: {$eq: 100}}]}

Elem match

The Elem match operator is used to match documents that contain an array field with at least one element that matches all the specified criteria.

from pyodmongo import DbEngine, MainBaseModel, DbModel
from typing import ClassVar
from pyodmongo.queries import elem_match

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(MainBaseModel):
    name: str
    price: float
    is_available: bool


class Order(DbModel):
    code: str
    products: list[Product]
    _collection: ClassVar = "orders"


query = elem_match(Product.name == "Box", Product.price == 50, field=Order.products)
box: Product = engine.find_one(Model=Product, query=query)

Equivalent filter in MongoDB

{$products: {$elemMatch: {name: "Box", price: 50}}}

Sort

The Sort operator arranges the results of a query in a specified order. It is useful for organizing data either in ascending or descending order based on a particular field.

from pyodmongo import DbEngine, DbModel
from typing import ClassVar
from pyodmongo.queries import sort

engine = DbEngine(mongo_uri="mongodb://localhost:27017", db_name="my_db")


class Product(DbModel):
    name: str
    price: float
    is_available: bool
    _collection: ClassVar = "products"


query = Product.price >= 10
my_sort = sort((Product.name, 1), (Product.price, -1))
box: Product = engine.find_one(Model=Product, query=query, sort=my_sort)

Equivalent filter in MongoDB

{name: 1, price: -1}