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, 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_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 {'field_name': {'$size': 3}}
fewer 2 {'field_name.1': {'$exists': False}}
minimum 5 {'field_name.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.1.1.tar.gz (24.0 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.1.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tag_query-1.1.1.tar.gz
  • Upload date:
  • Size: 24.0 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.1.1.tar.gz
Algorithm Hash digest
SHA256 3052860a430b05e7cc1a914a6edd77a07b9dd94f7536ab310f1d7d38e1339292
MD5 6f408b0fb49e192fb2d0211efe25d5de
BLAKE2b-256 013ccf86317183e6897d1fbb1db65b61b0a4fef0c048de77137318612b76a40a

See more details on using hashes here.

File details

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

File metadata

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

Hashes for tag_query-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 909cea89f1dcc1377f700f85648a589882cb4b8ab9523e90bbab17da71350b44
MD5 7b8f48900c621d64bddf8e5d12859c9f
BLAKE2b-256 efa30bc8103c4c879b64d4dbe8cc0e9113e9f6dfc2c23d7328fb19b46bfaddb3

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