Package: pymongoexplain
Info:Explain collections in PyMongo. SeeGitHubfor the latest source.Author:Julius ParkAboutThis package provides anExplainableCollectionclass that allows PyMongo’s Collection methods to beexplainedPyMongoExplain greatly simplifies the amount of effort needed to explain commands.
For example, suppose we wanted to explain the followingupdate_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}})InstallationTo install this package simply use pip:pip install pymongoexplainSupport / FeedbackFor questions, discussions, or general technical support, visit theMongoDB Community Forums.
The MongoDB Community Forums are a centralized place to connect with other MongoDB users, ask questions, and get answers.Bugs / Feature RequestsThink you’ve found a bug? Want to see a new feature in PyMongoExplain?
Please open an issue on thisGitHub repository.How To Ask For HelpPlease 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)"DependenciesPyMongoExplain requires CPython 3.5+, and PyPy3.5+.PyMongoExplain requiresPyMongo>=3.10,<4TestingThe easiest way to run the tests is to runpython setup.py testin
the root of the distribution.TutorialPyMongo operations in existing application code can be explained by swappingCollectionobjects withExplainableCollectionobjects. TheExplainableCollectionclass provides all CRUD API methods provided by PyMongo’sCollection,
but using this class to run operations runs explain on them, instead of executing them.To run explain on a command, first instantiate anExplainableCollectionfrom theCollectionobject 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 theExplainableCollectionconstructor like so:explain = ExplainableCollection(collection, verbosity="queryPlanner",
                                comment="I'm a comment")For more information see the documentation for theexplaincommand.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}})Nowresultwill contain the output of running explain on the givenupdate_onecommand:{'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'}}SinceExplainableCollectioninstances provide all the same methods provided byCollectioninstances, explaining operations in your application code is a simple matter of replacingCollectioninstances in your application code withExplainableCollectioninstances.Explaining commands in a scriptYou can also run explain on all commands within a Python script using our CLI tool.
Given a script that containspymongocommands 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 commandin the script itself. Do note that because the
explain output is generated using theloggingmodule,
if your script configures logging module there are certain things to keep in mind:if your script sets thelogging levelhigher 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]LimitationsThis package does not support the fluentCursor 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=...)