Skip to content

DOCSP-46425: Specify a Query #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 27, 2025
Merged

Conversation

norareidy
Copy link
Collaborator

@norareidy norareidy commented Jan 14, 2025

Copy link

netlify bot commented Jan 14, 2025

Deploy Preview for docs-django ready!

Name Link
🔨 Latest commit 75a555a
🔍 Latest deploy log https://app.netlify.com/sites/docs-django/deploys/6797b5ea5465260008d21d88
😎 Deploy Preview https://deploy-preview-2--docs-django.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

In this guide, you can learn how to use {+django-odm+} to specify
a database query.

The Django ``QuerySet`` API provides functions that allow you to retrieve
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/functions/methods/

a database query.

The Django ``QuerySet`` API provides functions that allow you to retrieve
model objects. To run a MongoDB database query, call ``QuerySet`` functions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/functions/methods/

to every model class.

When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
perform the operation until you evaluate the variable, usually by printing. After
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/,usually by printing//.

I would leave this out, more here about how you can evaluate: https://docs.djangoproject.com/en/5.1/ref/models/querysets/#when-querysets-are-evaluated

Run a Query
-----------

To query your MongoDB data, call a ``QuerySet`` function on your
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

model's ``Manager`` class and specify your matching criteria in a
query filter.

This section describes how to use the following functions from
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/functions/methods/

~~~~~~~~~~~~~~~~~~~~~~

To retrieve all documents from a collection, call the ``all()``
function on your model's ``Manager`` class.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

To retrieve all documents from a collection, call the ``all()``
function on your model's ``Manager`` class.

The following example calls the ``all()`` function to retrieve
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

~~~~~~~~~~~~~~~~~~~~~~~~~~~

To query a collection for documents that match a set of criteria,
call the ``filter()`` function on your model's ``Manager`` class.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/


To query a collection for documents that match a set of criteria,
call the ``filter()`` function on your model's ``Manager`` class.
Pass a query filter to the ``filter()`` function that specifies your
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

Pass a query filter to the ``filter()`` function that specifies your
query criteria.

The following example calls the ``filter()`` function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

~~~~~~~~~~~~~~~~~~~~~

To retrieve one document from a collection, call the ``get()``
function on your model's ``Manager`` class. Pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/


To retrieve one document from a collection, call the ``get()``
function on your model's ``Manager`` class. Pass
a query filter to the ``get()`` function that specifies your
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

.. important::

If your query matches no documents or multiple documents, the ``get()``
function generates an error. To retrieve one document from a query
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

If your query matches no documents or multiple documents, the ``get()``
function generates an error. To retrieve one document from a query
that might match multiple, use the :ref:`first() <django-query-first>`
function.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

function.


The following example calls the ``get()`` function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

~~~~~~~~~~~~~~~~~~~~~~~~~~

To query a collection for documents that do not meet your
search criteria, call the ``exclude()`` function on your model's
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

To query a collection for documents that do not meet your
search criteria, call the ``exclude()`` function on your model's
``Manager`` class. Pass the exclusion criteria as an
argument to the ``exclude()`` function.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/function/method/

Copy link
Collaborator

@aclark4life aclark4life left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending s/\b(functions?)\b/methods?/g

Also FWIW, methods are functions that belong to a class.

Copy link
Collaborator

@Jibola Jibola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I loved reading this! provided some additional guidance.

Comment on lines 442 to 445
You can combine field lookups to further specify your
query criteria. To combine lookups, chain each lookup
type together and separate them with a double underscore
(``__``).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is misleading. The example below is actually demonstrating both navigating relationships (Movie --> Award --> Text) and then doing a lookup (istartswith).

Additionally, there's a bit of confusion because Award is still a nested document, so while it does have a relationship, it's not the same way "relationships" are communicated in Django.

I.e.

  • Using a nested document shouldn't be treated as a relationship, but as a way to query embedded models
  • Using a ForeignKey that references a model, however, is different. That is a relationship spanning multiple collections (tables).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I made some changes - described Q objects in this section instead as a way to combine query filters, and moved the embedded model field section to the "Advanced Field Queries" section

Comment on lines 651 to 655
transform of a ``JSONField``. For example, the following code uses
the ``annotate()`` and ``KT()`` functions to create a new ``score`` key, which
stores ``imdb.rating`` nested field values. Then, the code sorts
each document in the ``sample_mflix.movies`` collection by
their descending ``score`` values:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding two tips:

  • One for a short explanation of KT -- used to work with the text value of keys, indexes, or path transforms within a JSONField. With a link to the KT documentation in Django
  • One for annotate (very powerful method!) -- Annotates each object in the QuerySet with the provided list of query expressions.

@norareidy norareidy requested a review from Jibola January 16, 2025 20:31
Copy link
Collaborator

@aclark4life aclark4life left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Collaborator

@Jibola Jibola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvements!

Added a couple more new suggestions. Let me know what you think.

Comment on lines +803 to +804
To learn how to run raw database queries by using MongoDB's aggregation
pipeline syntax, see the :ref:`django-raw-queries` guide.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. I think this blurb should get moved to the "Advanced Field Queries" section.
  2. We should still point to the django-raw-queries guide, but I think we should adopt something more like the language in Django's documentation.

The blurb could go something like this...

To leverage the power of querying in Django using the MongoDB Aggregation Pipeline directly, 
you can fall back on doing saw through our raw querying.
Check out our section on raw querying; see the :ref:`django-raw-queries` guide.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay added - but added a section to "Run a Query", since raw_aggregate() doesn't query a specific field type.

@norareidy norareidy requested a review from Jibola January 21, 2025 15:32
@rustagir rustagir self-requested a review January 22, 2025 16:24
Copy link

@rustagir rustagir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly small fixes + questions but great job!

When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
perform the operation until you evaluate the variable. After
evaluating the ``QuerySet``, Django saves the query results in the ``QuerySet``
cache. Future evaluations of the ``QuerySet`` reuse the cached results.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cache. Future evaluations of the ``QuerySet`` reuse the cached results.
cache. Future evaluations of the ``QuerySet`` use the cached results.

Comment on lines 40 to 41
To learn more about the Django ``QuerySet`` API, see `QuerySet API reference
<{+django-docs+}/ref/models/querysets/>`__ in the Django documentation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: indentation is off

Comment on lines 43 to 48
You can refine the set of documents that a query returns by creating a
**query filter**. A query filter is an expression that specifies the search
criteria MongoDB uses to match documents in a read or write operation.
In a query filter, you can prompt {+django-odm+} to search for documents with an
exact match to your query, or you can compose query filters to express more
complex matching criteria.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: I think that this paragraph should be further up. Maybe you can move the specifics about the QuerySet API into a subheading called Querying API?

Comment on lines 244 to 248
If you want to run complex queries that Django's query API
cannot express, you can use the ``raw_aggregate()`` method. This
method allows you to specify your query criteria in a MongoDB
aggregation pipeline, which you pass as an argument to
``raw_aggregate()``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to run complex queries that Django's query API
cannot express, you can use the ``raw_aggregate()`` method. This
method allows you to specify your query criteria in a MongoDB
aggregation pipeline, which you pass as an argument to
``raw_aggregate()``.
If you want to run complex queries that Django's query API
does not support, you can use the ``raw_aggregate()`` method. This
method allows you to specify your query criteria in a MongoDB
aggregation pipeline, which you pass as an argument to
``raw_aggregate()``.

.. _django-query-arrayfield:

Query an ArrayField
~~~~~~~~~~~~~~~~~

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: extend underline

Comment on lines 652 to 653
additional field lookup types for querying ``ArrayField`` values,
as described in the following table:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
additional field lookup types for querying ``ArrayField`` values,
as described in the following table:
additional field lookup types for querying ``ArrayField`` values,
which are described in the following table:

```````

The following example uses the ``__overlap`` lookup to query
for documents whose ``genres`` field stores any values in the

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for documents whose ``genres`` field stores any values in the
for documents whose ``genres`` field contains any values in the

Comment on lines 716 to 717
The following example queries the ``imdb`` field, a ``JSONField``,
for values of its nested ``votes`` field that exceed ``900000``:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The following example queries the ``imdb`` field, a ``JSONField``,
for values of its nested ``votes`` field that exceed ``900000``:
The following example queries the ``JSONField``-valued ``imdb`` field
for values of its nested ``votes`` field that exceed ``900000``:


The ``Movie`` model, which represents the ``sample_mflix.movies`` collection,
uses the ``id`` field as its primary key. The following example constructs
the same query as the preceding code:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the same query as the preceding code:
the same query as the preceding code by using the ``id`` field:

@norareidy norareidy requested a review from rustagir January 22, 2025 22:29
Copy link

@rustagir rustagir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few small suggestions but everything looks really good!

Comment on lines 122 to 128
<Movie: In the Land of the Head Hunters>, <Movie: The Perils of Pauline>,
<Movie: The Italian>, <Movie: Regeneration>, <Movie: Civilization>,
<Movie: Where Are My Children?>, <Movie: The Poor Little Rich Girl>,
<Movie: Wild and Woolly>, <Movie: The Blue Bird>, <Movie: From Hand to Mouth>,
<Movie: High and Dizzy>, <Movie: One Week>, <Movie: The Saphead>,
<Movie: The Ace of Hearts>, <Movie: The Four Horsemen of the Apocalypse>,
'...(remaining elements truncated)...']>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: applies to multiple examples, but up to you to truncate this further as the number of results doesn't signify anything

Comment on lines +61 to +65
.. literalinclude:: /includes/interact-data/specify-a-query.py
:start-after: start-models
:end-before: end-models
:language: python
:copyable:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: cant comment on the code example but it might be helpful to note either as a code comment or somewhere in this text that the str method means that the string representation of a model is just its title

Copy link
Collaborator

@R-shubham R-shubham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@norareidy norareidy merged commit a7004fe into mongodb:master Jan 27, 2025
5 checks passed
@norareidy norareidy deleted the DOCSP-46425-query branch January 27, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants