A Python package for robust querying of string lists in MongoDB.
Project description
A DSL for robust querying of tags in MongoDB
This module converts a text query into a dict that can be directly used in a MongoDB query of a string array field.
To get started, install this package with python3 -m pip install tag_query.
Example Usage
To query a collection for any documents for which the field field_name contains value1 and value2:
from tag_query import compile_query, exceptions
try:
mongo_query = compile_query(
expression = 'value1 and value2',
field = 'field_name'
)
# Or just `compile_query('value1 and value2', 'field_name')`
except exceptions.ParseError as e:
print(e)
exit(1)
print(mongo_query) #will output -> {'$and': [{'field_name': 'value1'}, {'field_name': 'value2'}]}
Syntax
First, it's important to note that a tag query expression has no precedence; expressions are evaluated from left to right, unless otherwise demarcated by parentheses.
- TAG: case-sensitive
- simple: Any non-keyword, non-function, alphanumeric text.
some_tagandThisIsATag123both count. - quoted: Any string of text inside double quotes.
"tag-with-dashes and operator text"is a single tag. - regex: Any string inside curly braces.
{^[A-Za-z0-9]+$}matches any purely alphanumeric tag. - CAVEATS for simple and quoted tags:
- If placed next to each other without an operator between them, the tags will concatenate with a single space as the delimiter. E.g.
tag1 and tag2 tag3is the same as"tag1" and "tag2 tag3" - The Glob operator (
*) may be used for simple pattern matching. E.g.*test*will match any tag that begins or ends with "test", such as "contested".*testortest*are also valid, and match tags that end and begin with "test", respectively.
- If placed next to each other without an operator between them, the tags will concatenate with a single space as the delimiter. E.g.
- simple: Any non-keyword, non-function, alphanumeric text.
- OPERATOR: case-insensitive
and,+: Require both values to exist.tag1 and tag2ortag1 + tag2or,/: Require at least 1 value to exist.tag1 or tag2ortag1 / tag2not,-: Invert the selection.not tag1ornot (tag1 and tag2)- In a binary expression,
notcan equaland not. For exampletag1 not tag2ortag1 - tag2
- In a binary expression,
- FUNCTION: case-insensitive
eq,equals,exact,exactly,=: Require the document to have exactly that many tags.exactly 5lt,fewer,below,<: Require the document to have fewer than that many tags.fewer 5gt,greater,above,>: Require the document to have more than that many tags.greater 5le,max,maximum,<=: Require the document to have at most that many tags.maximum 5ge,min,minimum,>=: Require the document to have at least that many tags.minimum 5
- PARENTHESES:
(,): Controls order of operations.a + (b - c)is different from(a + b) - c(the latter is the same asa + b - c. remember: left to right).
Examples
Here are some example tag queries and their corresponding outputs. The outputs can be directly passed to MongoDB as selection criteria.
| Query Expression | MongoDB Query Output |
|---|---|
tag1 and tag2 |
{'$and': [{'field_name': 'tag1'}, {'field_name': 'tag2'}]} |
tag1 or tag2 |
{'$or': [{'field_name': 'tag1'}, {'field_name': 'tag2'}]} |
not tag1 |
{'field_name': {'$ne': 'tag1'}} |
tag1 and not tag2 |
{'$and': [{'field_name': 'tag1'}, {'field_name': {'$ne': 'tag2'}}]} |
"tag with spaces" |
{'field_name': 'tag with spaces'} |
three tags concatenated |
{'field_name': 'three tags concatenated'} |
{^foo.*} |
{'field_name': {'$regex': '^foo.*'}} |
*test* |
{'field_name': {'$regex': 'test'}} |
exactly 3 |
{'tags': {'$size': 3}} |
fewer 2 |
{'tags.1': {'$exists': False}} |
minimum 5 |
{'tags.4': {'$exists': True}} |
tag1 or (tag2 and not tag3) |
{'$or': [{'field_name': 'tag1'}, {'$and': [{'field_name': 'tag2'}, {'field_name': {'$ne': 'tag3'}}]}]} |
You can use any of these expressions with compile_query(expression, field='field_name').
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
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 tag_query-1.0.1.tar.gz.
File metadata
- Download URL: tag_query-1.0.1.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afe9d6af8338fd684a49cb652c8104be4f59388b697554da4e40b52fd0bc77ba
|
|
| MD5 |
22ea48e2f52706f3a4ed1a9e4762d969
|
|
| BLAKE2b-256 |
8aba0f4041f53be628bfb4cb8d4d250a9c7fc759d882ec00f4378240be58f4cc
|
File details
Details for the file tag_query-1.0.1-py3-none-any.whl.
File metadata
- Download URL: tag_query-1.0.1-py3-none-any.whl
- Upload date:
- Size: 23.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5821d30022c18ea4cec30875d264ba1b20ef35e113c26bfd604122a65748cf3
|
|
| MD5 |
33e05bc6d70237aa8c1f55c3997bbb9b
|
|
| BLAKE2b-256 |
1c45ba28afc6db36337664fa2b918528a2ebc3812a6185c28736fdd6b438b365
|