Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 37
What is the behavior of MongoDB when sorting on a field that has both numeric and non-numeric values in the same collection?
-
A
The sort order is based on the lexicographical order of the values.
-
B
The sort fails and returns an error.
-
C
The sort order is based on the type of the first value encountered for each field.
-
D
The numeric values are sorted first, followed by the non-numeric values.
Reveal correct answer
Correct answer: A
A.
When sorting on a field that contains both numeric and non-numeric values in the same collection, MongoDB sorts the values based on their lexicographical order. In lexicographical order, the sorting is done character by character, which means that numbers are treated as strings. For example, if you have the values ["2", "10", "1"], they would be sorted as ["1", "10", "2"] because the sorting is based on the characters in the string representation of the numbers.
B.
MongoDB does not fail or return an error when sorting a field with both numeric and non-numeric values. It performs the sort operation based on the specified sort criteria, even if the values are of mixed types.
C.
MongoDB does not base the sort order on the type of the first value encountered for each field. The sorting behavior is consistent for all values in the field, regardless of the type of the first value.
D.
In MongoDB, when sorting on a field with mixed numeric and non-numeric values, the sort order is based on the lexicographical order of the values, not the type. Numeric and non-numeric values are intermixed and sorted based on their string representation.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
