Skip to content

DOCSP-35957: Retrieve guide #2722

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 26 commits into from
Mar 1, 2024
Merged

DOCSP-35957: Retrieve guide #2722

merged 26 commits into from
Mar 1, 2024

Conversation

norareidy
Copy link
Contributor

@norareidy norareidy commented Feb 5, 2024

Adds a find operations page to the docs.

JIRA: https://jira.mongodb.org/browse/DOCSP-35957
Staging: https://preview-mongodbnorareidy.gatsbyjs.io/laravel/DOCSP-35957/retrieve/

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

Copy link
Contributor

@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.

a few small things + questions

--------

In this guide, you can learn how to retrieve data from your MongoDB
collections using **find operations**. Find operations are commands that
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
collections using **find operations**. Find operations are commands that
collections by using **find operations**. Find operations are commands that

Pass the field name and value for which you are querying as parameters to ``where()``.

You can also pass a comparison operator as the ``where()`` method's second parameter,
such as the ``>`` operator. If you do not include a comparison operator parameter,
Copy link
Contributor

Choose a reason for hiding this comment

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

S: explain what > does

Suggested change
such as the ``>`` operator. If you do not include a comparison operator parameter,
such as the ``>`` operator to perform a "greater than" comparison. If you do not include a comparison operator parameter,

.. input::
:language: php

$movies = Movie::all();
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: does this code example also print the retrieved documents?

Copy link
Contributor Author

@norareidy norareidy Feb 6, 2024

Choose a reason for hiding this comment

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

The code snippet itself doesn't, but editing the view class according to the Quick Start instructions outputs the results in the format shown

Comment on lines 119 to 122
$movies =
Movie::where('year', 2010)
->where('imdb.rating', '>', 8.5)
->get();
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: same question here - does this code actually print the results?

Comment on lines 158 to 159
The following example returns one document in which the value of the ``title`` field
is ``"The Parent Trap"``:
Copy link
Contributor

Choose a reason for hiding this comment

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

S: could you use an example here for a query that would definitely match more than one document to show that the method matches only the first one?

Copy link
Contributor Author

@norareidy norareidy Feb 6, 2024

Choose a reason for hiding this comment

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

I used "The Parent Trap" because there are two versions (the original and the remake) and two matching documents - would it be better to have a query that's even more broad / matches more documents?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a query filter such as "year == 1961" or "runtime == 30" could provide a stronger hint that there's more than one result (without risk of causing an overload of results since the limit function hasn't been introduced).

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed!

- Controller class to store and run the find operations
- View class to display the results of the find operations

For instructions on creating these classes, see the Quick Start.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:
I think it would be good to add explanations or links to instructions on how to edit the files and view the results. I think the instructions could eventually be moved to a "Fundamentals" landing page. Happy to help with the content for this.

Comment on lines 149 to 153
To retrieve one document that matches a set of criteria, use the ``where()`` method
followed by the ``first()`` method.

For more information about the ``where()`` method and its parameters, see the
:ref:`laravel-find-all` section of this guide.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:
I think it would be helpful to mention sorting, even if showing how to sort is not shown on this page so that the reader can understand, from a set of results, which document to expect as the "first".

Find All Documents
------------------

To retrieve all documents in a collection, use the ``all()`` method.
Copy link
Contributor

Choose a reason for hiding this comment

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

Issue:

I think "use the ___ method" might not be clear as to what you're calling it on.

Suggestion:
I think it could be helpful to make the following changes:

  • Describe connection between "MongoDB collection" and "Laravel Model" in the Overview section.
  • Provide specifics on what the user should call the "all()" method on.

.. input::
:language: php

$movies = Movie::all();
Copy link
Contributor

Choose a reason for hiding this comment

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

Issue:
This relates to both "Find All Documents in a Collection Example" and "Find All Matching Documents Example".

I ran an issue retrieving large result sets in the Quick Start because the movies collection is large and the web app will time out trying to retrieve them. If it was able to retrieve them, displaying them on a webpage would probably cause memory issues for the browser.

Suggestion:
I think omitting the example could prevent users from running into the same issue. Instead of showing the example, describe the syntax of the method and alias and what it would do.

Instead of showing output, I think it would be better just to show the syntax of how to provide an empty filter (Movie::get()), which would help them understand how to chain other modifiers, and its alias (Movie::all()). Rather than showing the result sets, it would be helpful to add an admonition mentioning that sample sets such as the movies collection would be too large.

@norareidy norareidy requested a review from rustagir February 8, 2024 14:57
Copy link
Contributor

@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.

a few things!

Comment on lines 23 to 25
In this guide, you can learn how to retrieve data from your MongoDB
collections by using **find operations**. Find operations are commands that
retrieve documents from the server.
Copy link
Contributor

Choose a reason for hiding this comment

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

S: these sentences kind of go in circles. The first sentence says that you you can retrieve data by using find operations -> find operations retrieve data. I noticed this is the wording used on our other pages too.. I think you can use the phrase "find operations" first.

What about something like:

Suggested change
In this guide, you can learn how to retrieve data from your MongoDB
collections by using **find operations**. Find operations are commands that
retrieve documents from the server.
In this guide, you can learn how to use the Laravel package to perform find operations on your
MongoDB collections. Find operations allow you to retrieve documents based on criteria that you specify.

Comment on lines 29 to 32
- :ref:`laravel-retrieve-setup`
- :ref:`laravel-retrieve-matching`
- :ref:`laravel-modify-find`
- :ref:`laravel-addtl-info`
Copy link
Contributor

Choose a reason for hiding this comment

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

S: add some description to these sections like here

Comment on lines 132 to 136
.. warning::

The ``movies`` collection in the Atlas sample dataset stores a large amount of data.
Retrieving and displaying all documents in this collection may cause your web
application to time out.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.. warning::
The ``movies`` collection in the Atlas sample dataset stores a large amount of data.
Retrieving and displaying all documents in this collection may cause your web
application to time out.
.. warning::
The ``movies`` collection in the Atlas sample dataset stores a large amount of data.
Retrieving and displaying all documents in this collection might cause your web
application to time out.

Comment on lines 147 to 153
To learn more about sorting, see the following resources:

- :manual:`Natural order </reference/glossary/#std-term-natural-order>`
in the Server manual glossary
- `Ordering, Grouping, Limit and Offset
<https://laravel.com/docs/10.x/queries#ordering-grouping-limit-and-offset>`__ in the Laravel
documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

S: move into tip at the end of this section

Comment on lines 158 to 159
The following example returns one document in which the value of the ``title`` field
is ``"The Parent Trap"``:
Copy link
Contributor

Choose a reason for hiding this comment

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

agreed!

Comment on lines 183 to 187
.. important:: Required File Changes

To run the preceding code example, you must edit the ``MovieController.php`` file in your
``my-app`` application. Copy the code example and paste it into the ``show()`` function, replacing
any existing code inside this function.
Copy link
Contributor

Choose a reason for hiding this comment

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

S: similar to earlier comment, move up

Use Laravel Queries to Retrieve Documents
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Rather than on a model, you can run find operations on a query builder instance that
Copy link
Contributor

Choose a reason for hiding this comment

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

S: the first clause is a bit confusing

Suggested change
Rather than on a model, you can run find operations on a query builder instance that
You can run find operations on a query builder instance that represents your collection instead of running them on models.

Copy link
Contributor

@ccho-mongodb ccho-mongodb left a comment

Choose a reason for hiding this comment

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

Thanks for picking up this prototype page. After going through the staged content, I think the information could be easier to find with descriptive headings that don't overlap and separating out "how to run this" the into tabs.

I think the introductory sentences in each section could guide the readers better if they followed a consistent structure. Let me know if the suggestions I provided make sense.

Finally, while I didn't leave any other comments on this, I think any resource/link mentioned must be accompanied by information that connects it to what the reader should learn, especially when the content of the linked page doesn't provide that specific guidance.

Let me know if you want to pair on changes!

@GromNaN GromNaN added the docs label Feb 13, 2024

You can pass the following types of query filters to the ``where()`` method:

- ``where('<field name>', <value>)``: instructs MongoDB to return documents that match
Copy link
Member

Choose a reason for hiding this comment

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

I find it more explicit if the context of the function is provided. Otherwise they can think the function where is global.

Suggested change
- ``where('<field name>', <value>)``: instructs MongoDB to return documents that match
- ``Model::where('<field name>', <value>)``: instructs MongoDB to return documents that match

Copy link
Contributor

@ccho-mongodb ccho-mongodb left a comment

Choose a reason for hiding this comment

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

Looks pretty good, thanks for making all the changes! Most of the suggestions are straightforward, but let me know if there's anything you'd like to discuss and/or whether you want another review.

Overview
--------

In this guide, you can learn how to use the Laravel package to perform **find operations**
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think "Laravel MongoDB" (the odm-short source constant) could be more consistent with the product name:

Suggested change
In this guide, you can learn how to use the Laravel package to perform **find operations**
In this guide, you can learn how to use the {+odm-short+} to perform **find operations**

Comment on lines 36 to 46
Complete the :ref:`Quick Start <laravel-quick-start>` tutorial before running the examples in this
guide.

After completing the Quick Start, ensure that your MongoDB deployment and Laravel application
satisfy the following requirements:

- Your Atlas MongoDB deployment contains the Atlas sample data
- ``Movie.php`` file contains a ``Movie`` model to represent the ``movies`` collection
- ``MovieController.php`` file contains a ``show()`` function to run database operations
- ``browse_movies.blade.php`` file contains HTML code to display the results of database
operations
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think the "Prerequisites" heading should describe a condition/state rather than provide instructions. One potential solution could be to replace the heading title with "Before You Get Started" and explain the following similar to this:

"To run the code in this guide, you need to complete the Quick Start tutorial, which includes setting up a MongoDB Atlas instance with sample data and the following files in your Laravel web application:

  • Movie.php, which contains the Movie model that ...
    "


Use the following syntax to specify the query:

.. code-block:: go
Copy link
Contributor

Choose a reason for hiding this comment

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

Issue:

I think "go" syntax highlighting might be compatible in this case, but it would be better to identify this as php code.

Suggestion:

I think using "php" would be more consistent.

Applies to all instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops didn't notice that - fixed!

to only return documents that meet these requirements. To run the query, call the ``where()``
method on an Eloquent model or query builder that represents your collection.

You can pass the following types of query filters to the ``where()`` method:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think the description of the "types" in "types of query filters" is missing from the list items. I think describing the list items directly like the following would help guide the reader:

"You can use one of the following "where()" method calls to build a query:"


.. _laravel-retrieve-one:

Retrieve Only One Result
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think "Return the First Result" could be more descriptive since the section doesn't describe how to retrieve any other type of a single result (such as the last result).

Applies to the description contained in this section.

- ``skip()``: sets the number of documents to skip when returning results
- ``take()``: sets the total number of documents to return
- ``first()``: returns the first document that matches the query filter

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:
I think the subheadings could be introduced, e.g.
"The following sections show examples of how to use these methods."

Comment on lines 391 to 393
matching document according to the documents' natural order, or as they appear in the
database. If you apply a sort to the find operation, ``first()`` returns the first matching
document according to the sorted order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think it would be better to recommend a consistent sort order before describing natural ordering.

E.g. "Use the sort() method when you use the first() to get consistent results when you query on a unique value. If you omit the sort() method, MongoDB returns the matching documents according to the documents' natural order."


Then, make the following changes to your Laravel Quick Start application:

- Add the ``use Illuminate\Support\Facades\DB`` use statement to your ``MovieController.php``
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think it could be good to match this with the other mention of this import in the section description since both versions of the example use it. The other instance currently reads:
"import the DB facade into your controller file"


.. note::

This change is necessary because you cannot use the ``->`` object operator to access document fields when running
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion:

I think it could be better if the admonition identified which change it refers to.
E.g.

"Since the Laravel query builder returns ... the view accesses the fields by using the ... syntax instead of the "->" object operator."

Copy link
Contributor

@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.

some small suggestions but LGTM! great work on this long PR!

tutorial. This tutorial provides instructions on setting up a MongoDB Atlas instance with
sample data and creating the following files in your Laravel web application:

- ``Movie.php`` file, which contains a ``Movie`` model to represent the ``movies`` collection
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- ``Movie.php`` file, which contains a ``Movie`` model to represent the ``movies`` collection
- ``Movie.php`` file, which contains a ``Movie`` model to represent documents in the ``movies`` collection

operations

The following sections describe how to edit the files in your Laravel application to run
the code examples and view the expected output.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
the code examples and view the expected output.
the find operation code examples and view the expected output.


Chain the ``orderBy()`` method to ``first()`` to get consistent results when you query on a unique
value. If you omit the ``orderBy()`` method, MongoDB returns the matching documents according to
the documents' natural order, or as they appear in the database.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
the documents' natural order, or as they appear in the database.
the documents' natural order, or as they appear in the collection.

Comment on lines 410 to 411
This example returns one document in which the value of the ``runtime`` field
is ``30``.
Copy link
Contributor

Choose a reason for hiding this comment

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

S: also describe that this code orders by _id

@norareidy norareidy marked this pull request as ready for review February 29, 2024 15:51
@norareidy norareidy requested a review from a team as a code owner February 29, 2024 15:51
@norareidy norareidy requested review from GromNaN and removed request for a team February 29, 2024 15:51
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

LGTM after a small fix


Use the following syntax to specify the query:

.. code-block:: go
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. code-block:: go
.. code-block:: php

@GromNaN GromNaN merged commit 2829bbc into mongodb:4.1 Mar 1, 2024
@GromNaN
Copy link
Member

GromNaN commented Mar 1, 2024

Thank you @norareidy

@GromNaN GromNaN mentioned this pull request Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants