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
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
Release files for mgqpy 0.8.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 | |
|---|---|---|---|
| mgqpy-0.8.0.tar.gz | 6.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mgqpy-0.8.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.7 kB
Release files / mgqpy-0.8.0.tar.gz
| Download URL | mgqpy-0.8.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c256dfc5b31b16c5dc7d642bc6e90372b4f0bb90e93d282584695374dbd91299
|
|
BLAKE2b-256 checksum How to use checksums |
9a3d9242e1ff7b0042d225916a559241ee57ca4750c883553d7a7310a5aeb857
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mgqpy-0.8.0-py3-none-any.whl
| Download URL | mgqpy-0.8.0-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6519a74a7758ec4ce1f05a0873006402e7130165a88fd3f8e9557427c6acf56
|
|
BLAKE2b-256 checksum How to use checksums |
228b40c96ee06ca4f3df86e5acd842999e2f552366b8d2a404e3417638d2bca3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|