How to ORDER BY FIELD VALUE in MongoDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

db.getCollection('form').aggregate([{
'$project': {
'name': 1,
'code': 1,
'handlerUserId': 1,
'handlerName': 1,
'reviewerUserId': 1,
'reviewerName': 1,
'createdUserId': 1,
'createdUserName': 1,
'address': 1,
'tnNumber': 1,
'createdDate': 1,
'updatedDate': 1,
'description': 1,
'formId': 1,
'formNo': 1,
'showFormNo': 1,
'jsonObject': 1,
'formTypeId': 1,
'version': 1,
'supervisorStatus': 1,
'supervisorStatusId':
{
$switch:
{
branches: [
{
case: { $eq : [ '$supervisorStatus', "FORM_NOT_SUBMITTED" ] },
then: 5
},
{
case: { $eq : [ '$supervisorStatus', "FORM_PROCESSING" ] },
then: 4
},
{
case: { $eq : [ '$supervisorStatus', "FORM_SUBMITTED_FOR_REVIEW" ] },
then: 3
},
{
case: { $eq : [ '$supervisorStatus', "FORM_APPROVED" ] },
then: 2
},
{
case: { $eq : [ '$supervisorStatus', "FORM_AUDIT_WAS_REJECTED" ] },
then: 1
}

],
default: 0
}
}
}
},
{
"$sort": {
"weight": -1
}
},
{ "$skip" : 0},

{ "$limit" : 20},

])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ProjectionOperation projectOperation = project("name", "code", "handlerUserId", "handlerName", "reviewerUserId", "reviewerName", "createdUserId", "createdUserName", "address", "tnNumber", "createdDate", "updatedDate", "description", "formId", "formNo", "showFormNo", "supervisorStatus", "formTypeId", "version")
.and(ConditionalOperators.switchCases(
ConditionalOperators.Switch.CaseOperator.when(
ComparisonOperators.Eq.valueOf("$supervisorStatus").equalToValue(FormConstant.FORM_STATUS_APPROVED.getKey()))
.then(5),
ConditionalOperators.Switch.CaseOperator.when(
ComparisonOperators.Eq.valueOf("$supervisorStatus").equalToValue(FormConstant.FORM_STATUS_AUDIT_WAS_REJECTED.getKey()))
.then(4),
ConditionalOperators.Switch.CaseOperator.when(
ComparisonOperators.Eq.valueOf("$supervisorStatus").equalToValue(FormConstant.FORM_STATUS_SUBMITTED_FOR_REVIEW.getKey()))
.then(3),
ConditionalOperators.Switch.CaseOperator.when(
ComparisonOperators.Eq.valueOf("$supervisorStatus").equalToValue(FormConstant.FORM_STATUS_NOT_SUBMITTED.getKey()))
.then(2),
ConditionalOperators.Switch.CaseOperator.when(
ComparisonOperators.Eq.valueOf("$supervisorStatus").equalToValue(FormConstant.FORM_STATUS_PROCESSING.getKey()))
.then(1)).defaultTo(0)
).as("supervisorStatusId");
SortOperation sortOperation = sort(DESC, "supervisorStatusId");
SkipOperation skipOperation = new SkipOperation(formListSearchDto.getOffset());
LimitOperation limitOperation = limit(formListSearchDto.getLimit());
TypedAggregation<Form> agg = newAggregation(Form.class,
projectOperation,
sortOperation,
skipOperation,
limitOperation
);
AggregationResults<Form> result = mongoTemplate.aggregate(agg, "form", Form.class);
List<Form> stateStatsList = result.getMappedResults();
long count = this.mongoTemplate.count(query, Form.class);
page = new PageImpl(stateStatsList, pageable, count);
# java
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×