File tree 2 files changed +17
-16
lines changed
2 files changed +17
-16
lines changed Original file line number Diff line number Diff line change @@ -47,24 +47,23 @@ module.exports = async function aggregate(collection, params) {
47
47
const $sort = generateSort ( params ) ;
48
48
const $limit = params . limit + 1 ;
49
49
50
- let addFields = [ ] ;
51
- let cleanUp = [ ] ;
50
+ let aggregation ;
52
51
if ( params . sortCaseInsensitive ) {
53
- addFields = [ { $addFields : { __lc : { $toLower : '$' + params . paginatedField } } } ] ;
54
- cleanUp = [ { $project : { __lc : 0 } } ] ;
52
+ aggregation = params . aggregation . concat ( [
53
+ { $addFields : { __lc : { $toLower : '$' + params . paginatedField } } } ,
54
+ { $match } ,
55
+ { $sort } ,
56
+ { $limit } ,
57
+ { $project : { __lc : 0 } } ,
58
+ ] ) ;
59
+ } else {
60
+ aggregation = params . aggregation . concat ( [ { $match } , { $sort } , { $limit } ] ) ;
55
61
}
56
- const aggregation = params . aggregation . concat ( [
57
- ...addFields ,
58
- { $match } ,
59
- { $sort } ,
60
- { $limit } ,
61
- ...cleanUp ,
62
- ] ) ;
63
62
64
63
// Aggregation options:
65
64
// https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#aggregate
66
65
// https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html
67
- const options = { ... params . options } ;
66
+ const options = Object . assign ( { } , params . options ) ;
68
67
/**
69
68
* IMPORTANT
70
69
*
Original file line number Diff line number Diff line change @@ -39,10 +39,12 @@ module.exports = async function(collection, params) {
39
39
let response ;
40
40
if ( params . sortCaseInsensitive ) {
41
41
// For case-insensitive sorting, we need to work with an aggregation:
42
- response = aggregate ( collection , {
43
- ...params ,
44
- aggregation : params . query ? [ { $match : params . query } ] : [ ] ,
45
- } ) ;
42
+ response = aggregate (
43
+ collection ,
44
+ Object . assign ( { } , params , {
45
+ aggregation : params . query ? [ { $match : params . query } ] : [ ] ,
46
+ } )
47
+ ) ;
46
48
} else {
47
49
// Need to repeat `params.paginatedField` default value ('_id') since it's set in 'sanitizeParams()'
48
50
params = _ . defaults ( await sanitizeParams ( collection , params ) , { query : { } } ) ;
You can’t perform that action at this time.
0 commit comments