Skip to main content

Explainable CRUD API for PyMongo

Project description

Info:

Explain collections in PyMongo. See GitHub for the latest source.

Author:

Julius Park

About

This package provides an ExplainableCollection class that allows PyMongo’s Collection methods to be explained

PyMongoExplain greatly simplifies the amount of effort needed to explain commands. For example, suppose we wanted to explain the following update_one:

collection.update_one({"quantity": 1057, "category": "apparel"},{"$set": {"reorder": True}})

Before PyMongoExplain, one would need to convert the update_one into the equivalent MongoDB command:

collection.database.command(SON([('explain', SON([('update', 'products'), ('updates', [{'q': {'quantity': 1057, 'category': 'apparel'}, 'upsert': False, 'multi': False, 'u': {'$set': {'reorder': True}}}])])), ('verbosity', 'queryPlanner')]))

After PyMongoExplain:

ExplainableCollection(collection).update_one({"quantity": 1057, "category": "apparel"},{"$set": {"reorder": True}})

Installation

To install this package simply use pip:

pip install pymongoexplain

Support / Feedback

For questions, discussions, or general technical support, visit the MongoDB Community Forums. The MongoDB Community Forums are a centralized place to connect with other MongoDB users, ask questions, and get answers.

Bugs / Feature Requests

Think you’ve found a bug? Want to see a new feature in PyMongoExplain? Please open an issue on this GitHub repository.

How To Ask For Help

Please include all of the following information when opening an issue:

  • Detailed steps to reproduce the problem, including full traceback, if possible.

  • The exact python version used, with patch level:

    $ python -c "import sys; print(sys.version)"
  • The exact version of PyMongo used (if applicable), with patch level:

    $ python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"
  • The exact version of PyMongoExplain used:

    $ python -c "import pymongoexplain; print(pymongoexplain.version)"

Dependencies

PyMongoExplain requires CPython 3.5+, and PyPy3.5+.

PyMongoExplain requires PyMongo>=3.10,<4

Testing

The easiest way to run the tests is to run python setup.py test in the root of the distribution.

Tutorial

PyMongo operations in existing application code can be explained by swapping Collection objects with ExplainableCollection objects. The ExplainableCollection class provides all CRUD API methods provided by PyMongo’s Collection, but using this class to run operations runs explain on them, instead of executing them.

To run explain on a command, first instantiate an ExplainableCollection from the Collection object originally used to run the command:

from pymongoexplain import ExplainableCollection

collection = client.db.products
explain = ExplainableCollection(collection)

If you wish to configure the options for the explain command itself, pass them to the ExplainableCollection constructor like so:

explain = ExplainableCollection(collection, verbosity="queryPlanner",
                                comment="I'm a comment")

For more information see the documentation for the explain command.

Now you are ready to explain some commands. Remember that explaining a command does not execute it:

result = explain.update_one({"quantity": 1057, "category": "apparel"}, {"$set": {"reorder": True}})

Now result will contain the output of running explain on the given update_one command:

{'ok': 1.0,
 'operationTime': Timestamp(1595603051, 3),
 'queryPlanner': {'indexFilterSet': False,
                  'namespace': 'db.products',
                  'parsedQuery': {'$and': [{'category': {'$eq': 'apparel'}},
                                           {'quantity': {'$eq': 1057}}]},
                  'planCacheKey': 'CD8F6D8F',
                  'plannerVersion': 1,
                  'queryHash': 'CD8F6D8F',
                  'rejectedPlans': [],
                  'winningPlan': {'inputStage': {'direction': 'forward',
                                                 'filter': {'$and': [{'category': {'$eq': 'apparel'}},
                                                                     {'quantity': {'$eq': 1057}}]},
                                                 'stage': 'COLLSCAN'},
                                  'stage': 'UPDATE'}},
 'serverInfo': {'gitVersion': '27f5c1ee9f513f29fe30b8ebefed99581428c6e1',
                'host': 'Juliuss-MBP.verizon.net',
                'port': 27017,
                'version': '4.4.0-rc13'}}

Since ExplainableCollection instances provide all the same methods provided by Collection instances, explaining operations in your application code is a simple matter of replacing Collection instances in your application code with ExplainableCollection instances.

Explaining commands in a script

You can also run explain on all commands within a Python script using our CLI tool. Given a script that contains pymongo commands within it, you can simply run:

python3 -m pymongoexplain <path/to/your/script.py>

This will log the explain output for every single command within the specified script, in addition to running every command in the script itself. Do note that because the explain output is generated using the logging module, if your script configures logging module there are certain things to keep in mind:

  • if your script sets the logging level higher than INFO, the explain output will be suppressed entirely.

  • the explain output will be sent to whatever stream your script configures the logging module to send output to.

Any positional parameters or arguments required by your script can be simply be appended to the invocation as follows:

python3 -m pymongoexplain <path/to/your/script.py> [PARAMS] [--optname OPTS]

Limitations

This package does not support the fluent Cursor API, so if you attempt to use it like so:

ExplainableCollection(collection).find({}).sort(...)

Instead pass all the arguments to the find() call, like so:

ExplainableCollection(collection).find({}, sort=...)

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

pymongoexplain-1.2.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

pymongoexplain-1.2.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file pymongoexplain-1.2.0.tar.gz.

File metadata

  • Download URL: pymongoexplain-1.2.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for pymongoexplain-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c6a4743fd1818467f7418363e91a83c2532026869efd820e894ea9f3c32dff5a
MD5 e0cf4cf0ca3b3952569e35a5959b4bb8
BLAKE2b-256 e364fcef55a6e392cc9ba9e3641de1eca6dda5860fe296cf0bb14d5fc8f2fa4a

See more details on using hashes here.

Provenance

File details

Details for the file pymongoexplain-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pymongoexplain-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for pymongoexplain-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b464a4a6fbc34269e7f61f283c3bd84bb20db10c5dc2ffc954632767b8129e4f
MD5 86a0b245753c641ecc67088d6173d00a
BLAKE2b-256 63a2948b4ae10a6890725f067a75626d5b4b480ce93d35e2ee26dee43bbfc67d

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page