mongo query as a predicate function
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mgqpy-0.7.0.tar.gz.
File metadata
- Download URL: mgqpy-0.7.0.tar.gz
- Upload date:
- Size: 158.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4d0396f28d6a8e743cbc63c213c208c7b8bdc53b89af76bbbdf046f555e30bf
|
|
| MD5 |
2f72d857067cd045f9a08302cf1173c6
|
|
| BLAKE2b-256 |
3c8e51c02e8f5b349bc2eda4a9de761f229e88b0d7e07b50ad2729e0819fb804
|
File details
Details for the file mgqpy-0.7.0-py2.py3-none-any.whl.
File metadata
- Download URL: mgqpy-0.7.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd333c47e37124401a58dd3f5f5850255b2ad4a389e59bc9097764df2b9a4f0e
|
|
| MD5 |
2e0f4227dbb9dfd0289923b4c5588a03
|
|
| BLAKE2b-256 |
8a09aeef7fafc043659b722050728b49d2c438e9539c11efeda2aa65515e3b67
|