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.0.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.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tag_query-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 e7a0bcf0bc1ebf5402939933c72a425491365272996b2f2bde3e1f8f2f35e6be
MD5 bdbeba89e6284892aac3dad3b86210f8
BLAKE2b-256 906667d08d8d70269d9a1a829ec8c46cd2c148b9e4d6817e4592de4e0d857c23

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tag_query-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9733c92ffc028ad2f2a6f729ccc0b1a69460b627041d3056e748c1827ccef85
MD5 4cb49f815331a8e89ca17d89d521e4f5
BLAKE2b-256 1023ac7e8651c34134b4b7b367110a9cb0f071200f07e79b0f29f9c8949e188f

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