What does the value -1 in cursor.sort() signify?

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.

In MongoDB, when using cursor.sort(), the value -1 indicates that the documents should be sorted in descending order based on the specified field. This means that if you apply sort({field: -1}), the documents will be returned starting from the highest value of the given field down to the lowest.

Using -1 can be particularly useful in scenarios where you want to retrieve the most recent documents first, such as when sorting by timestamps. In contrast, a value of 1 would indicate ascending order, which organizes the documents from the smallest value up to the largest. The other options do not apply in the context of this sorting functionality, as they refer to either no sorting or other types of order that are not a part of the sort specification in MongoDB.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy