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 matches the 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.match, 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 match if you wish to validate and test against an input in one go.

predicate.validate().match({"foo": 1})

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.6.2.tar.gz (158.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.6.2-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for mgqpy-0.6.2.tar.gz
Algorithm Hash digest
SHA256 4a9498b8e9eb25e5a8eae8efbc3fdb7b41142a97d053b2a316309419bafb6fec
MD5 d7924d712cf766fd29192fe1d492e181
BLAKE2b-256 4a5bc571e9506e3a96c9aa85043a8c8716374a2d531bc53733b215238fccd46c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mgqpy-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c630c3d6f0d4050a65e7e2ad5516691bc9301189f4f5977839c4cd3ce0a75eb
MD5 04b6b938d3e20a9ce055e68d0c0178f8
BLAKE2b-256 5e9fa9b20668c512fc21eb981255336898f6c9bbf04f4ff84867bdcb988d1b64

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