Skip to main content

A utility library that provides a MongoDB-like query language for querying python collections. It's mainly intended to parse objects structured as fundamental types in a similar fashion to what is produced by JSON or YAML parsers.

Project description

https://github.com/reuben/mongoquery/actions/workflows/test.yml/badge.svg

A utility library that provides a MongoDB-like query language for querying python collections. It’s mainly intended to parse objects structured as fundamental types in a similar fashion to what is produced by JSON or YAML parsers. It follows the specification of queries for MongoDB version 3.4.

Usage

Example:

from mongoquery import Query, QueryError

records = [
    {"a": 5, "b": 5, "c": None},
    {"a": 3, "b": None, "c": 8},
    {"a": None, "b": 3, "c": 9},
    {"a": 1, "b": 2, "c": 3},
    {"a": 2, "c": 5},
    {"a": 3, "b": 2},
    {"a": 4},
    {"b": 2, "c": 4},
    {"b": 2},
    {"c": 6}
]

a_is_3 = Query({"a": 3})

# match a single object
matched = a_is_3.match(records[1])
assert matched

matched = a_is_3.match(records[0])
assert not matched

# filter elements
filtered = filter(
    a_is_3.match,
    records
)
assert filtered == [records[1], records[5]]

# incorrect filters raise QueryError
try:
    matched = query({"$foo": 2}).match(records[1])
except QueryError:
    pass  # => "$foo" operator isn't supported

Query syntax

The syntax of queries matches closely the one of MongoDB queries, and translates it to python using the following rules:

  • operators and field identifiers are expressed as strings. For instance, the following MongoDB query:

    {
        memos: {
            $elemMatch: {
                memo: 'on time',
                by: 'billing'
            }
        }
    }

    Translates straightforwardly to the following Python dict:

    {
        "memos": {
            "$elemMatch": {
                "memo": 'on time',
                "by": 'billing'
            }
        }
    }
  • regular expressions are expected to be expressed as string containing the MongoDB regular expression syntax. For instance:

    {description: {$regex: /^S/m}}

    Translates to the following Python dict:

    {"description": {"$regex": "/^S/m"}}
  • the boolean, null syntax used in MongoDB follows the JavaScript syntax. It is expected the python equivalents are used. For instance:

    {a: {$exists: true}, b: null}

    Translates to the following Python dict:

    {"a": {"$exists": True}, "b": None}

Functional differences with MongoDB’s queries

There are a few features that are not supported by mongoquery:
  • Only the "/pattern/<options>" syntax is supported for $regex. As a consequence, $options isn’t supported. Alternatively you can compile a regex using re.compile and supply options as parameters, and pass the re.Pattern object as the value of $regex.

  • $text hasn’t been implemented.

  • Due to the pure python nature of this library, $where isn’t supported.

  • The Geospatial operators $geoIntersects, $geoWithin, $nearSphere, and $near are not implemented.

  • Projection operators $`, $elemMatch, $meta, and $slice are not implemented (only querying is implemented)

  • $type is limited to recognising generic python types, it won’t look into recognising the format of the data (for instance, it doesn’t check Object ID’s format, only that they are strings)

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

mongo_query_match-2.0.0.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

mongo_query_match-2.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file mongo_query_match-2.0.0.tar.gz.

File metadata

  • Download URL: mongo_query_match-2.0.0.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for mongo_query_match-2.0.0.tar.gz
Algorithm Hash digest
SHA256 8ed8d079163c44abb46ef2c40b4a0ffa3f1ef58b53884040213f5576481fd03c
MD5 1c65215ffe4007989224481b86969cce
BLAKE2b-256 9bc657ecb8c01cc40ac01e9e5449d0b3561c70e4a99e8ee05395efe4798b2e18

See more details on using hashes here.

File details

Details for the file mongo_query_match-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mongo_query_match-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97c8b94cc080cc545540416a045fcb5a591acff06629f26fd06cd95df816b8b3
MD5 40e26da6b4207e946d69bf999f3b5659
BLAKE2b-256 72555263d7e273e630dfe2c1017003f1241694dcf0d512e792cfb88d7f5a1207

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