Skip to main content

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, include this repo as a submodule in your project. Then you can use it directly. For example, 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_tag and ThisIsATag123 both 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 tag3 is 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". *test or test* are also valid, and match tags that end and begin with "test", respectively.
  • OPERATOR: case-insensitive
    • and, +: Require both values to exist. tag1 and tag2 or tag1 + tag2
    • or, /: Require at least 1 value to exist. tag1 or tag2 or tag1 / tag2
    • not, -: Invert the selection. not tag1 or not (tag1 and tag2)
      • In a binary expression, not can equal and not. For example tag1 not tag2 or tag1 - tag2
  • FUNCTION: case-insensitive
    • eq,equals,exact,exactly, =: Require the document to have exactly that many tags. exactly 5
    • lt,fewer,below, <: Require the document to have fewer than that many tags. fewer 5
    • gt,greater,above, >: Require the document to have more than that many tags. greater 5
    • le,max,maximum, <=: Require the document to have at most that many tags. maximum 5
    • ge,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 as a + 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

tag_query-1.0.0.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tag_query-1.0.0-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file tag_query-1.0.0.tar.gz.

File metadata

  • Download URL: tag_query-1.0.0.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

Hashes for tag_query-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b0cd64499513f051bf4f51d55aeafc70b1ded4453dc98dc4b748a917094a698e
MD5 5079c6b8025f716e8cffcbcaecc11868
BLAKE2b-256 4436ca25f598922d2541fb7eb496916b75130199b4dd3594f61348f162628496

See more details on using hashes here.

File details

Details for the file tag_query-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tag_query-1.0.0-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

Hashes for tag_query-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97f7cc098419abdecc4ecece1fde07818901912fcb99a20e72f156a1e4b268b9
MD5 513b1209acac6a3c0f1272937d4b22b9
BLAKE2b-256 f50a0c9e54f6cd51bc3f4ae77c8a249276fba97412dba6a932e4141a985fb875

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