Skip to content

beanie.odm.operators.find.array

All

class All(BaseFindArrayOperator)

$all array query operator

Example:

class Sample(Document):
    results: List[int]

All(Sample.results, [80, 85])

Will return query object like

{"results": {"$all": [80, 85]}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/all

ElemMatch

class ElemMatch(BaseFindArrayOperator)

$elemMatch array query operator

Example:

class Sample(Document):
    results: List[int]

ElemMatch(Sample.results, [80, 85])

Will return query object like

{"results": {"$elemMatch": [80, 85]}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

Size

class Size(BaseFindArrayOperator)

$size array query operator

Example:

class Sample(Document):
    results: List[int]

Size(Sample.results, 2)

Will return query object like

{"results": {"$size": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/size/

beanie.odm.operators.find.logical

Or

class Or(LogicalOperatorForListOfExpressions)

$or query operator

Example:

class Product(Document):
    price: float
    category: str

Or({Product.price<10}, {Product.category=="Sweets"})

Will return query object like

{"$or": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/or/

And

class And(LogicalOperatorForListOfExpressions)

$and query operator

Example:

class Product(Document):
    price: float
    category: str

And({Product.price<10}, {Product.category=="Sweets"})

Will return query object like

{"$and": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/and/

Nor

class Nor(BaseFindLogicalOperator)

$nor query operator

Example:

class Product(Document):
    price: float
    category: str

Nor({Product.price<10}, {Product.category=="Sweets"})

Will return query object like

{"$nor": [{"price": {"$lt": 10}}, {"category": "Sweets"}]}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/nor/

Not

class Not(BaseFindLogicalOperator)

$not query operator

Example:

class Product(Document):
    price: float
    category: str

Not({Product.price<10})

Will return query object like

{"$not": {"price": {"$lt": 10}}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/not/

beanie.odm.operators.find.comparison

Eq

class Eq(BaseFindComparisonOperator)

equal query operator

Example:

class Product(Document):
    price: float

Eq(Product.price, 2)

Will return query object like

{"price": 2}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/eq/

GT

class GT(BaseFindComparisonOperator)

$gt query operator

Example:

class Product(Document):
    price: float

GT(Product.price, 2)

Will return query object like

{"price": {"$gt": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/gt/

GTE

class GTE(BaseFindComparisonOperator)

$gte query operator

Example:

class Product(Document):
    price: float

GTE(Product.price, 2)

Will return query object like

{"price": {"$gte": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/gte/

In

class In(BaseFindComparisonOperator)

$in query operator

Example:

class Product(Document):
    price: float

In(Product.price, [2, 3, 4])

Will return query object like

{"price": {"$in": [2, 3, 4]}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/in/

NotIn

class NotIn(BaseFindComparisonOperator)

$nin query operator

Example:

class Product(Document):
    price: float

NotIn(Product.price, [2, 3, 4])

Will return query object like

{"price": {"$nin": [2, 3, 4]}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/nin/

LT

class LT(BaseFindComparisonOperator)

$lt query operator

Example:

class Product(Document):
    price: float

LT(Product.price, 2)

Will return query object like

{"price": {"$lt": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/lt/

LTE

class LTE(BaseFindComparisonOperator)

$lte query operator

Example:

class Product(Document):
    price: float

LTE(Product.price, 2)

Will return query object like

{"price": {"$lte": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/lte/

NE

class NE(BaseFindComparisonOperator)

$ne query operator

Example:

class Product(Document):
    price: float

NE(Product.price, 2)

Will return query object like

{"price": {"$ne": 2}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/ne/

beanie.odm.operators.find.bitwise

BitsAllClear

class BitsAllClear(BaseFindBitwiseOperator)

$bitsAllClear query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/bitsAllClear/

BitsAllSet

class BitsAllSet(BaseFindBitwiseOperator)

$bitsAllSet query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/bitsAllSet/

BitsAnyClear

class BitsAnyClear(BaseFindBitwiseOperator)

$bitsAnyClear query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/bitsAnyClear/

BitsAnySet

class BitsAnySet(BaseFindBitwiseOperator)

$bitsAnySet query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/bitsAnySet/

beanie.odm.operators.find.element

Exists

class Exists(BaseFindElementOperator)

$exists query operator

Example:

class Product(Document):
    price: float

Exists(Product.price, True)

Will return query object like

{"price": {"$exists": True}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/exists/

Type

class Type(BaseFindElementOperator)

$type query operator

Example:

class Product(Document):
    price: float

Type(Product.price, "decimal")

Will return query object like

{"price": {"$type": "decimal"}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/type/

beanie.odm.operators.find.geospatial

GeoIntersects

class GeoIntersects(BaseFindGeospatialOperator)

$geoIntersects query operator

Example:

class GeoObject(BaseModel):
    type: str = "Point"
    coordinates: Tuple[float, float]

class Place(Document):
    geo: GeoObject

    class Collection:
        name = "places"
        indexes = [
            [("geo", pymongo.GEOSPHERE)],  # GEO index
        ]

GeoIntersects(Place.geo, "Polygon", [[0,0], [1,1], [3,3]])

Will return query object like

{
    "geo": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [[0,0], [1,1], [3,3]],
            }
        }
    }
}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/geoIntersects/

GeoWithin

class GeoWithin(BaseFindGeospatialOperator)

$geoWithin query operator

Example:

class GeoObject(BaseModel):
    type: str = "Point"
    coordinates: Tuple[float, float]

class Place(Document):
    geo: GeoObject

    class Collection:
        name = "places"
        indexes = [
            [("geo", pymongo.GEOSPHERE)],  # GEO index
        ]

GeoWithin(Place.geo, "Polygon", [[0,0], [1,1], [3,3]])

Will return query object like

{
    "geo": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [[0,0], [1,1], [3,3]],
            }
        }
    }
}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/geoWithin/

Near

class Near(BaseFindGeospatialOperator)

$near query operator

Example:

class GeoObject(BaseModel):
    type: str = "Point"
    coordinates: Tuple[float, float]

class Place(Document):
    geo: GeoObject

    class Collection:
        name = "places"
        indexes = [
            [("geo", pymongo.GEOSPHERE)],  # GEO index
        ]

Near(Place.geo, 1.2345, 2.3456, min_distance=500)

Will return query object like

{
    "geo": {
        "$near": {
            "$geometry": {
                "type": "Point",
                "coordinates": [1.2345, 2.3456],
            },
            "$maxDistance": 500,
        }
    }
}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/near/

NearSphere

class NearSphere(Near)

$nearSphere query operator

Example:

class GeoObject(BaseModel):
    type: str = "Point"
    coordinates: Tuple[float, float]

class Place(Document):
    geo: GeoObject

    class Collection:
        name = "places"
        indexes = [
            [("geo", pymongo.GEOSPHERE)],  # GEO index
        ]

NearSphere(Place.geo, 1.2345, 2.3456, min_distance=500)

Will return query object like

{
    "geo": {
        "$nearSphere": {
            "$geometry": {
                "type": "Point",
                "coordinates": [1.2345, 2.3456],
            },
            "$maxDistance": 500,
        }
    }
}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/nearSphere/

beanie.odm.operators.find.evaluation

Expr

class Expr(BaseFindEvaluationOperator)

$type query operator

Example:

class Sample(Document):
    one: int
    two: int

Expr({"$gt": [ "$one" , "$two" ]})

Will return query object like

{"$expr": {"$gt": [ "$one" , "$two" ]}}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/expr/

JsonSchema

class JsonSchema(BaseFindEvaluationOperator)

$jsonSchema query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/

Mod

class Mod(BaseFindEvaluationOperator)

$mod query operator

Example:

class Sample(Document):
    one: int

Mod(Sample.one, 4, 0)

Will return query object like

{ "one": { "$mod": [ 4, 0 ] } }

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/mod/

RegEx

class RegEx(BaseFindEvaluationOperator)

$regex query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/regex/

Text

class Text(BaseFindEvaluationOperator)

$text query operator

Example:

class Sample(Document):
    description: Indexed(str, pymongo.TEXT)

Text("coffee")

Will return query object like

{
    "$text": {
        "$search": "coffee" ,
        "$caseSensitive": False,
        "$diacriticSensitive": False
    }
}

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/text/

Text.__init__

 | __init__(search: str, language: Optional[str] = None, case_sensitive: bool = False, diacritic_sensitive: bool = False)

Arguments:

  • search: str
  • language: Optional[str] = None
  • case_sensitive: bool = False
  • diacritic_sensitive: bool = False

Where

class Where(BaseFindEvaluationOperator)

$where query operator

MongoDB doc: https://docs.mongodb.com/manual/reference/operator/query/where/