Skip to main content

mongo query as a predicate function

Project description

mgqpy

codecov PyPI - Version

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.2.tar.gz (159.4 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.2-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for mgqpy-0.7.2.tar.gz
Algorithm Hash digest
SHA256 558d03211379038df4a8e08a93fd3cb19de7641760d98a7a21950e318b0db65f
MD5 e91c801a0c961478ebb2fe7f75e3a235
BLAKE2b-256 1d521de629cbe95583fc6832a5633f691ab65bf60e447c3ee5efbbd873e02f1c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mgqpy-0.7.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1936a11c1ee4272deb8af73420135e21792eec85f6ae5896829eab08befac54
MD5 131a821a6c18fbad06b91bcc056801e0
BLAKE2b-256 f4d885a909f5f6b927cbd251dde6bc84c2d457cd3b02d61e63078ce49cbe5b33

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