2
2
Sorts Builders
3
3
==============
4
4
5
- .. default-domain:: mongodb
5
+ .. facet::
6
+ :name: genre
7
+ :values: reference
8
+
9
+ .. meta::
10
+ :keywords: code example, sorting
6
11
7
12
.. contents:: On this page
8
13
:local:
@@ -43,12 +48,12 @@ contains the following documents:
43
48
44
49
.. code-block:: json
45
50
46
- {"_id": 1, "letter ": "c ", "food ": "coffee with milk"}
47
- {"_id": 3 , "letter ": "a ", "food ": "maple syrup"}
48
- {"_id": 4 , "letter ": "b ", "food ": "coffee with sugar"}
49
- {"_id": 5 , "letter ": "a ", "food ": "milk and cookies"}
50
- {"_id": 2 , "letter ": "a ", "food ": "donuts and coffee"}
51
- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
51
+ { "_id": 1, "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" },
52
+ { "_id": 2 , "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" },
53
+ { "_id": 3 , "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" },
54
+ { "_id": 4 , "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" },
55
+ { "_id": 5 , "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" },
56
+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
52
57
53
58
The Sorts Class
54
59
---------------
@@ -92,9 +97,9 @@ The output of the preceding example should look something like this:
92
97
.. code-block:: json
93
98
:copyable: false
94
99
95
- {"_id": 1, "letter ": "c ", "food ": "coffee with milk" }
96
- {"_id": 2, "letter ": "a ", "food ": "donuts and coffee" }
97
- {"_id": 3, "letter ": "a ", "food ": "maple syrup" }
100
+ { "_id": 1, "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
101
+ { "_id": 2, "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" }
102
+ { "_id": 3, "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" }
98
103
...
99
104
100
105
Descending
@@ -120,9 +125,9 @@ The preceding example should output something like this:
120
125
.. code-block:: json
121
126
:copyable: false
122
127
123
- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
124
- {"_id": 5, "letter ": "a ", "food ": "milk and cookies" }
125
- {"_id": 4, "letter ": "b ", "food ": "coffee with sugar" }
128
+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
129
+ { "_id": 5, "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
130
+ { "_id": 4, "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" }
126
131
...
127
132
128
133
Combining Sort Criteria
@@ -146,20 +151,20 @@ on the ``letter`` field, and in the event of a tie, ascending order on the
146
151
147
152
// <MongoCollection setup code here>
148
153
149
- Bson orderBySort = orderBy(descending("letter "), ascending("_id "));
154
+ Bson orderBySort = orderBy(descending("date "), ascending("orderTotal "));
150
155
collection.find().sort(orderBySort);
151
156
152
157
The output of the preceding example should look something like this:
153
158
154
159
.. code-block:: json
155
160
:copyable: false
156
161
157
- {"_id": 1 , "letter ": "c ", "food ": "coffee with milk" }
158
- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
159
- {"_id": 4, "letter ": "b ", "food ": "coffee with sugar" }
160
- {"_id": 2 , "letter ": "a ", "food ": "donuts and coffee" }
161
- {"_id": 3 , "letter ": "a ", "food ": "maple syrup" }
162
- {"_id": 5 , "letter ": "a ", "food ": "milk and cookies" }
162
+ { "_id": 5 , "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
163
+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
164
+ { "_id": 4, "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" }
165
+ { "_id": 3 , "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" }
166
+ { "_id": 2 , "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" }
167
+ { "_id": 1 , "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
163
168
164
169
Text Score
165
170
----------
0 commit comments