Skip to main content

mongo query as a predicate function

Project description

mgqpy

codecov

MongoDB query as a predicate function

This aims to be consistent with how MongoDB's matches documents. This includes traversal across nested dicts and lists, None and field-presence/absence handling.

Installation

pip install mgqpy

Usage

Test if an input satisfies a query.

Invalid queries (e.g. using $and without a list) will automatically return False.

Use the validate method to raise errors if there's a problem with the query.

from mgqpy import Query

predicate = Query({"foo.bar": {"$gt": 1}})

inputs = [
    {"foo": [{"bar": [1, 2]}]},
    {"foo": {"bar": 1}},
    {"foo": {"bar": 2}},
    {"foo": None},
]

filtered = filter(predicate.test, inputs)

assert list(filtered) == [
    {"foo": [{"bar": [1, 2]}]},
    {"foo": {"bar": 2}},
]

Use validate to validate queries given.

predicate = Query({"foo": {"$in": 1}})

try:
    predicate.validate()
except TypeError as e:
    # ...

validate returns the Query instance so you can chain test if you wish to validate and test against an input in one go.

input = {"foo": 1}

predicate.validate().test(input)

Supported operators

Comparison query operators

  • $eq
  • $eq (implicit), e.g. {"foo": None}
  • $ne
  • $gt
  • $gte
  • $lt
  • $lte
  • $in
  • $nin

Logical query operators

  • $and
  • $and (implicit), e.g. {"foo": 1, "bar": "baz"}
  • $or
  • $not
  • $nor

Evaluation query operators

  • $regex
  • $regex (implicit), e.g. {"foo": re.compile('^bar')}
  • $mod

Array query operators

  • $all
  • $elemMatch
  • $size

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mgqpy-0.7.1.tar.gz (158.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mgqpy-0.7.1-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mgqpy-0.7.1.tar.gz.

File metadata

  • Download URL: mgqpy-0.7.1.tar.gz
  • Upload date:
  • Size: 158.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for mgqpy-0.7.1.tar.gz
Algorithm Hash digest
SHA256 a2b9495fcbc7ae6bf1379c46ba79985486b028c599a17db8bac0611c00b9019c
MD5 5c0380e539d31e119ed21b6f531b1bbb
BLAKE2b-256 3ae6bd88f83242464f53ae93416e0a853435a477dca0d374635516fd378f5f2c

See more details on using hashes here.

File details

Details for the file mgqpy-0.7.1-py2.py3-none-any.whl.

File metadata

  • Download URL: mgqpy-0.7.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for mgqpy-0.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 493045b962803c7e10096b660d78dcd6655bfb9d82d967b3ff42a9223a3e1915
MD5 d4d2aed8b395985280ce274b8393868b
BLAKE2b-256 4e069a5add364f16ca2e463b9d72a41093f8f4dd57f4998b841d1516c278f631

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page