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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: mgqpy-0.6.1.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.1.tar.gz
Algorithm Hash digest
SHA256 6dee728cf1096d2a6f971fe0d5143762e7b68949f0bbb59b86c79b217079405e
MD5 ae717c5d35dc6603967a5286bf61a018
BLAKE2b-256 1a2009c14d203a14f2bbcbe1e62248768f8f17b44cc0b9e3fea5f31b63e9794d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mgqpy-0.6.1-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.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c017f3b3e531e4182c4869ca70b564f740147266d81aa00e55ae13623066a3f3
MD5 665edf2355c85f875265545374db7410
BLAKE2b-256 fd2a83ba802cc8b632cf6811ecae42d924c72af143301754b0c91b550b1bfb58

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