Skip to content

Commit 147c936

Browse files
committed
mongoclient operations
1 parent 257eb85 commit 147c936

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ api = "https://django-mongodb.readthedocs.io/en/latest/"
1515
mdb-server = "MongoDB Server"
1616
django-version = "5.0"
1717
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"
18+
pymongo-docs = "https://www.mongodb.com/docs/languages/python/pymongo-driver/current/"

source/interact-data/raw-queries.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Django adds a ``Manager`` named ``objects`` to every model class. This default
4747
MongoDB-specific method, set your model's ``objects`` field to a custom
4848
manager called ``MongoManager``.
4949

50+
As an alternative to the ``raw_aggregate()`` method, you can run queries directly
51+
on your ``MongoClient`` object for expanded access to your MongoDB data.
52+
5053
Sample Data
5154
~~~~~~~~~~~
5255

@@ -293,6 +296,33 @@ code performs the following actions:
293296
Theater ID: 814
294297
Theater ID: 323
295298

299+
.. _django-client-operations:
300+
301+
MongoClient Operations
302+
----------------------
303+
304+
If you want to run database operations that neither the ``QuerySet`` API
305+
nor the ``raw_aggregate()`` method provide, you can operate on your
306+
``MongoClient`` directly. When working with the ``MongoClient``, you can
307+
can access the PyMongo driver's database operations. Use the following
308+
syntax to expose the ``MongoClient``:
309+
310+
.. code-block::
311+
:language: python
312+
313+
from django.db import connections
314+
315+
client = connections["<DATABASES key>"].database.client
316+
317+
Replace the ``"<DATABASES key>"`` placeholder with the key in your
318+
``DATABASES`` dictionary that corresponds to your target database. To
319+
use your default database, replace the placeholder with ``"default"``.
320+
321+
.. tip::
322+
323+
To learn how to use PyMongo to interact with MongoDB data,
324+
see the `PyMongo documentation. <{+pymongo-docs+}>`__
325+
296326
Additional Information
297327
----------------------
298328

0 commit comments

Comments
 (0)