|
1 |
| -This package provides an ExplainCollection class |
2 |
| -that allows common MongoDB commands to be explained |
| 1 | +This package provides an ``ExplainCollection`` class |
| 2 | +that allows that allows PyMongo's Collection methods to be explained_ |
| 3 | + |
| 4 | +.. _explained: https://docs.mongodb.com/master/reference/command/explain/#dbcmd.explain. |
| 5 | + |
| 6 | + |
| 7 | +Tutorial |
| 8 | +######## |
| 9 | + |
| 10 | +PyMongo operations in existing application code can be explained by swapping ``Collection`` objects with ``ExplainCollection`` |
| 11 | +objects. The ``ExplainCollection`` class provides all CRUD API methods provided by PyMongo's ``Collection``, |
| 12 | +but using this class to run operations runs explain on them, instead of executing them. |
| 13 | + |
| 14 | +To run explain on a command, first instantiate an ``ExplainCollection`` from the ``Collection`` object originally used to run the command:: |
| 15 | + |
| 16 | + collection = client.db.products |
| 17 | + explain = ExplainCollection(collection) |
| 18 | + |
| 19 | +Now you are ready to explain some commands. Remember that explaining a command does not execute it:: |
| 20 | + |
| 21 | + result = explain.update_one({"quantity": 1057, "category": "apparel"}, {"$set": {"reorder": True}}) |
| 22 | + |
| 23 | +Now ``result`` will contain the output of running explain on the given ``update_one`` command:: |
| 24 | + |
| 25 | + {'$clusterTime': |
| 26 | + {'signature': { |
| 27 | + 'keyId': 0, 'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}, |
| 28 | + 'clusterTime': Timestamp(1595603051, 3)}, |
| 29 | + 'serverInfo': {'host': 'Juliuss-MBP.verizon.net', 'version': '4.4.0-rc13', 'port': 27017, 'gitVersion': '27f5c1ee9f513f29fe30b8ebefed99581428c6e1'}, |
| 30 | + 'queryPlanner': |
| 31 | + {'plannerVersion': 1, |
| 32 | + 'queryHash': 'CD8F6D8F', |
| 33 | + 'parsedQuery': |
| 34 | + {'$and': [ |
| 35 | + {'category': {'$eq': 'apparel'}}, |
| 36 | + {'quantity': {'$eq': 1057}}]}, |
| 37 | + 'namespace': 'db.products', |
| 38 | + 'indexFilterSet': False, |
| 39 | + 'winningPlan': |
| 40 | + {'inputStage': |
| 41 | + {'filter': |
| 42 | + {'$and': [ |
| 43 | + {'category': {'$eq': 'apparel'}}, |
| 44 | + {'quantity': {'$eq': 1057}}]}, |
| 45 | + 'stage': 'COLLSCAN', 'direction': 'forward'}, |
| 46 | + 'stage': 'UPDATE'}, 'planCacheKey': 'CD8F6D8F', |
| 47 | + 'rejectedPlans': []}, 'ok': 1.0, |
| 48 | + 'operationTime': Timestamp(1595603051, 3)} |
| 49 | + |
| 50 | + |
| 51 | +Since ``ExplainCollection`` 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 ``ExplainCollection`` instances. |
| 52 | + |
| 53 | + |
| 54 | +Explaining commands in a script |
| 55 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 56 | + |
| 57 | +You can also run explain on all commands within a Python script using our CLI tool. |
| 58 | +Given a script that contains ``pymongo`` commands within it, you can simply run: :: |
| 59 | + |
| 60 | + python3 -m pymongoexplain <path/to/your/script.py> |
| 61 | + |
| 62 | +This will print out the explain output for every single command |
| 63 | +within that script, in addition to running the script itself. |
| 64 | + |
| 65 | +Any positional parameters or arguments required by your script can be |
| 66 | +simply be appended to the invocation as follows:: |
| 67 | + |
| 68 | + python3 -m pymongoexplain <path/to/your/script.py> [PARAMS] [--optname OPTS] |
0 commit comments