-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
✅ Deploy Preview for docs-django ready!
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/function/method/
There was a problem hiding this 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.
There was a problem hiding this 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.
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 | ||
(``__``). |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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.
To learn how to run raw database queries by using MongoDB's aggregation | ||
pipeline syntax, see the :ref:`django-raw-queries` guide. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things:
- I think this blurb should get moved to the "Advanced Field Queries" section.
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache. Future evaluations of the ``QuerySet`` reuse the cached results. | |
cache. Future evaluations of the ``QuerySet`` use the cached results. |
To learn more about the Django ``QuerySet`` API, see `QuerySet API reference | ||
<{+django-docs+}/ref/models/querysets/>`__ in the Django documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: indentation is off
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. |
There was a problem hiding this comment.
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?
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()``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | ||
~~~~~~~~~~~~~~~~~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: extend underline
additional field lookup types for querying ``ArrayField`` values, | ||
as described in the following table: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for documents whose ``genres`` field stores any values in the | |
for documents whose ``genres`` field contains any values in the |
The following example queries the ``imdb`` field, a ``JSONField``, | ||
for values of its nested ``votes`` field that exceed ``900000``: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same query as the preceding code: | |
the same query as the preceding code by using the ``id`` field: |
There was a problem hiding this 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!
<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)...']> |
There was a problem hiding this comment.
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
.. literalinclude:: /includes/interact-data/specify-a-query.py | ||
:start-after: start-models | ||
:end-before: end-models | ||
:language: python | ||
:copyable: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
JIRA - https://jira.mongodb.org/browse/DOCSP-46425
Staging - https://deploy-preview-2--docs-django.netlify.app/interact-data/specify-a-query/