mgqpy
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
Development
Install development dependencies with uv sync --locked --dev. Tests compare
results against MongoDB 7 on localhost:27017. Outside an Amp orb, start it with
docker compose up --detach --wait, then run uv run pytest.
Amp orbs use .agents/setup to install the locked Python dependencies and a
native MongoDB 7 server into the reusable project snapshot. .agents/resume
ensures the supervised MongoDB service is running after activation and wake;
it does not reinstall dependencies. MongoDB listens only on loopback, requires
no credentials, and stores disposable test data in .amp/mongo/.
Use amp orb services ensure to start or repair it manually and
amp orb service logs mongo to inspect its logs.
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, including an operator mgqpy does not support (e.g. $type, or $elemMatch with operators on values like {"$elemMatch": {"$gt": 1}}). test cannot evaluate these and usually returns False for them.
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
Element query operators
- $exists
Evaluation query operators
- $regex
- $regex (implicit), e.g.
{"foo": re.compile('^bar')} - $mod
Array query operators
- $all
- $elemMatch
- $size
Metadata
Release files for mgqpy 0.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mgqpy-0.9.0.tar.gz | 7.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mgqpy-0.9.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.1 kB
Release files / mgqpy-0.9.0.tar.gz
| Download URL | mgqpy-0.9.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b99752f987f00424dbd934886d8fb0b18b563f9fc4961d7ed73487bc65ab508
|
|
BLAKE2b-256 checksum How to use checksums |
fe999ed0a4d62770da879f49d13e8031844bbcf4b8e004297201ec64234c5db4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mgqpy-0.9.0-py3-none-any.whl
| Download URL | mgqpy-0.9.0-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b28092f525942991c9f9e9e6f15466e14e28f2d95bc5c3b3b89e8968619f2f8
|
|
BLAKE2b-256 checksum How to use checksums |
82244375ba85a146da5a639f9322aa3fdf83013d2041670cd1f41d0176662ad9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for mgqpy 0.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.