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}},
]

Validating the query given.

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

from mgqpy import Query

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

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

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

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for mgqpy-0.6.0.tar.gz
Algorithm Hash digest
SHA256 7b933fafba2993159bc71a84b73d15bad8ede2088f97b30c2a56275a66fdc126
MD5 ab931d60a42ad18c0b56af69673668b6
BLAKE2b-256 81d3b695f9281c71130b15e4d52f6c4171ca817492b86cee48f7e2c84f14722e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mgqpy-0.6.0-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.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3ddc0edafc42ae9ede5120cb1972f1fbf65255f6e51f6ce6c99eaa5f0df2d907
MD5 9cd4e914e06b02e10ed1f0e14f82a1c9
BLAKE2b-256 d923b14f790ad03269c04856ff20b94615f4bee6e0a7e7c105764b609b317bd4

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