mongoquery


Namemongoquery JSON
Version 1.4.2 PyPI version JSON
download
home_pagehttp://github.com/kapouille/mongoquery
SummaryA 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.
upload_time2022-05-24 12:39:12
maintainer
docs_urlNone
authorOlivier Carrere
requires_python
license
keywords mongodb query match
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ==========
mongoquery
==========

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

A utility library that provides a `MongoDB <http://www.mongodb.org>`_-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:

.. code-block:: python

    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 <http://docs.mongodb.org/manual/tutorial/query-documents/>`_,
and translates it to python using the following rules:

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

    .. code-block:: javascript

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

    Translates straightforwardly to the following Python dict:

    .. code-block:: python

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

  - regular expressions are expected to be expressed as string containing
    the MongoDB regular expression syntax. For instance:

    .. code-block:: javascript

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

    Translates to the following Python dict:

    .. code-block:: python

      {"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:

    .. code-block:: javascript

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

    Translates to the following Python dict:

    .. code-block:: python

      {"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)

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/kapouille/mongoquery",
    "name": "mongoquery",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "mongodb query match",
    "author": "Olivier Carrere",
    "author_email": "olivier.carrere@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/98/07/e14a0e556f6610a36c28c03b988d54d62704e0f095cb1fe428f8b7602dc0/mongoquery-1.4.2.tar.gz",
    "platform": null,
    "description": "==========\nmongoquery\n==========\n\n.. image:: https://travis-ci.org/kapouille/mongoquery.svg?branch=master\n    :target: https://travis-ci.org/kapouille/mongoquery\n\nA utility library that provides a `MongoDB <http://www.mongodb.org>`_-like query\nlanguage for querying python collections. It's mainly intended to parse objects\nstructured as fundamental types in a similar fashion to what is produced by `JSON`\nor `YAML` parsers. It follows the specification of queries for MongoDB version 3.4.\n\n\n-----\nUsage\n-----\n\nExample:\n\n.. code-block:: python\n\n    from mongoquery import Query, QueryError\n\n    records = [\n        {\"a\": 5, \"b\": 5, \"c\": None},\n        {\"a\": 3, \"b\": None, \"c\": 8},\n        {\"a\": None, \"b\": 3, \"c\": 9},\n        {\"a\": 1, \"b\": 2, \"c\": 3},\n        {\"a\": 2, \"c\": 5},\n        {\"a\": 3, \"b\": 2},\n        {\"a\": 4},\n        {\"b\": 2, \"c\": 4},\n        {\"b\": 2},\n        {\"c\": 6}\n    ]\n\n    a_is_3 = Query({\"a\": 3})\n\n    # match a single object\n    matched = a_is_3.match(records[1])\n    assert matched\n\n    matched = a_is_3.match(records[0])\n    assert not matched\n\n    # filter elements\n    filtered = filter(\n        a_is_3.match,\n        records\n    )\n    assert filtered == [records[1], records[5]]\n\n    # incorrect filters raise QueryError\n    try:\n        matched = query({\"$foo\": 2}).match(records[1])\n    except QueryError:\n        pass  # => \"$foo\" operator isn't supported\n\n\n------------\nQuery syntax\n------------\n\nThe syntax of queries matches closely the one of\n`MongoDB queries <http://docs.mongodb.org/manual/tutorial/query-documents/>`_,\nand translates it to python using the following rules:\n\n  - operators and field identifiers are expressed as strings. For instance,\n    the following MongoDB query:\n\n    .. code-block:: javascript\n\n      {\n          memos: {\n              $elemMatch: {\n                  memo: 'on time',\n                  by: 'billing'\n              }\n          }\n      }\n\n    Translates straightforwardly to the following Python dict:\n\n    .. code-block:: python\n\n      {\n          \"memos\": {\n              \"$elemMatch\": {\n                  \"memo\": 'on time',\n                  \"by\": 'billing'\n              }\n          }\n      }\n\n  - regular expressions are expected to be expressed as string containing\n    the MongoDB regular expression syntax. For instance:\n\n    .. code-block:: javascript\n\n      {description: {$regex: /^S/m}}\n\n    Translates to the following Python dict:\n\n    .. code-block:: python\n\n      {\"description\": {\"$regex\": \"/^S/m\"}}\n\n  - the boolean, null syntax used in MongoDB follows the JavaScript syntax.\n    It is expected the python equivalents are used. For instance:\n\n    .. code-block:: javascript\n\n      {a: {$exists: true}, b: null}\n\n    Translates to the following Python dict:\n\n    .. code-block:: python\n\n      {\"a\": {\"$exists\": True}, \"b\": None}\n\n\n---------------------------------------------\nFunctional differences with MongoDB's queries\n---------------------------------------------\n\nThere are a few features that are not supported by ``mongoquery``:\n    - Only the ``\"/pattern/<options>\"`` syntax is supported for ``$regex``. As\n      a consequence, ``$options`` isn't supported. Alternatively you can\n      compile a regex using ``re.compile`` and supply options as parameters, and pass\n      the ``re.Pattern`` object as the value of ``$regex``.\n    - ``$text`` hasn't been implemented.\n    - Due to the pure python nature of this library, ``$where`` isn't supported.\n    - The `Geospatial` operators ``$geoIntersects``, ``$geoWithin``,\n      ``$nearSphere``, and ``$near`` are not implemented.\n    - Projection operators `$``, ``$elemMatch``, ``$meta``, and ``$slice`` are\n      not implemented (only querying is implemented)\n    - ``$type`` is limited to recognising generic python types, it won't look\n      into recognising the format of the data (for instance, it doesn't check\n      Object ID's format, only that they are strings)\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "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.",
    "version": "1.4.2",
    "project_urls": {
        "Homepage": "http://github.com/kapouille/mongoquery"
    },
    "split_keywords": [
        "mongodb",
        "query",
        "match"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "de3ce63127c743f6a1f7cc64c28956f104174da618eb4fbc5a3a1ce7c3bb018d",
                "md5": "b8a98d8cf58973dc8de8cc6d67253ccf",
                "sha256": "5f7536b0e5b66ef79766b671af0b492836094101c6179c9f5f1b5968412ea6a2"
            },
            "downloads": -1,
            "filename": "mongoquery-1.4.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b8a98d8cf58973dc8de8cc6d67253ccf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6319,
            "upload_time": "2022-05-24T12:39:11",
            "upload_time_iso_8601": "2022-05-24T12:39:11.461206Z",
            "url": "https://files.pythonhosted.org/packages/de/3c/e63127c743f6a1f7cc64c28956f104174da618eb4fbc5a3a1ce7c3bb018d/mongoquery-1.4.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9807e14a0e556f6610a36c28c03b988d54d62704e0f095cb1fe428f8b7602dc0",
                "md5": "98968d6e2463505520f70634178caec1",
                "sha256": "bd19fc465f0aa9feb3070f144fde41fc68cf28ea32dd3b7565f7df3ab6fc0ac2"
            },
            "downloads": -1,
            "filename": "mongoquery-1.4.2.tar.gz",
            "has_sig": false,
            "md5_digest": "98968d6e2463505520f70634178caec1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6331,
            "upload_time": "2022-05-24T12:39:12",
            "upload_time_iso_8601": "2022-05-24T12:39:12.802910Z",
            "url": "https://files.pythonhosted.org/packages/98/07/e14a0e556f6610a36c28c03b988d54d62704e0f095cb1fe428f8b7602dc0/mongoquery-1.4.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-05-24 12:39:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kapouille",
    "github_project": "mongoquery",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "lcname": "mongoquery"
}
        
Elapsed time: 0.08215s