What query should be used to find the longest trips taken by subscribers from the trips collection, returning the top 5 results?

Prepare for the MongoDB Associate Developer Exam with flashcards and practice multiple choice questions. Each question includes hints and explanations to ensure you're ready for test day.

The correct choice involves using a query that filters documents in the trips collection to only include those where the usertype is "Subscriber." After filtering, it sorts these documents based on the tripduration in descending order. The use of sort({ tripduration: -1 }) ensures that the trips with the longest durations appear first in the results. Finally, the limit(5) function restricts the output to the top 5 longest trips.

By first applying the filter for "Subscriber," we ensure that the subsequent sorting and limiting operate only on this subset, which is essential for accurately identifying the top 5 longest trips taken specifically by subscribers. The ordering in descending format (-1) is crucial since we want the longest trips, while limiting the results to five correctly focuses on our specific interest.

In contrast, looking at the other options reveals that they either sort in ascending order, would return a larger or unordered set of results, or fail to apply the necessary usertype filter. Therefore, the chosen query is indeed the most effective for meeting the stated requirement.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy