Skip to main content

mongoquery

https://travis-ci.org/kapouille/mongoquery.svg?branch=master

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)

Release files for mongoquery 1.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongoquery 1.4.3
File Size Uploaded
mongoquery-1.4.3.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongoquery 1.4.3
File Interpreter ABI Platform
mongoquery-1.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / mongoquery-1.4.3.tar.gz

Download URL mongoquery-1.4.3.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e901f86ee5af7ef6eda2f2c26f4bd2cfe8380d81f178b7e3607dbbcdcee97239
BLAKE2b-256 checksum
How to use checksums
f1ec8e0ddb347c6c5b94e0147b2b9ea1fd9dbfda5d222d0e529996445c30faaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / mongoquery-1.4.3-py3-none-any.whl

Download URL mongoquery-1.4.3-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8be47635c5e361c9f363b7a901a2b86649687491b7aaca2d12601eff41cb6128
BLAKE2b-256 checksum
How to use checksums
ce337ce3f09d91a0ca5db7cca67671d865b7c8310b8b59eff287a711919641f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

1.4.3 This release

2 release files

1.4.2

2 release files

1.4.0

2 release files

1.3.6

1 release file

1.3.5

2 release files

1.3.4

1 release file

1.3.3

1 release file

1.3.2

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

0.1.3

1 release file

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page