Reorder the sorting
MODIFY
src/storage/mongodb/AssetStorage.ts
src/storage/mongodb/AssetStorage.ts
@@ -147,15 +147,12 @@ export default class AssetStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });
@@ -247,15 +244,12 @@ export default class AssetStorage {
// Add Created By / Last Changed By
DatabaseUtils.pushCreatedLastChangedInAggregation(tenantID, aggregation);
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -109,15 +109,12 @@ export default class BillingStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -91,15 +91,12 @@ export default class CarStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });
@@ -630,20 +627,17 @@ export default class CarStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
- aggregation.push({
- $sort: dbParams.sort
- });
- } else {
- aggregation.push({
- $sort: {
+ if (!dbParams.sort) {
+ dbParams.sort = {
'carCatalog.vehicleMake': 1,
'carCatalog.vehicleModel': 1,
'carCatalog.vehicleModelVersion': 1,
'licensePlate': 1,
+ };
}
+ aggregation.push({
+ $sort: dbParams.sort
});
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });
@@ -833,15 +827,12 @@ export default class CarStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { 'user.name': 1, 'user.firstName': 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { 'user.name': 1, 'user.firstName': 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -247,11 +247,7 @@ export default class ChargingStationStorage {
aggregation.pop();
// Sort
if (!dbParams.sort) {
- dbParams.sort = {
- $sort: {
- _id: 1
- }
- };
+ dbParams.sort = { _id: 1 };
}
aggregation.push({
$sort: dbParams.sort
@@ -385,17 +381,12 @@ export default class ChargingStationStorage {
// Add Created By / Last Changed By
DatabaseUtils.pushCreatedLastChangedInAggregation(tenantID, aggregation);
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { _id: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: {
- _id: 1
- }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -705,18 +696,15 @@ export default class ChargingStationStorage {
// Rename ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
- if (dbParams.sort) {
- aggregation.push({
- $sort: dbParams.sort
- });
- } else {
- aggregation.push({
- $sort: {
+ if (!dbParams.sort) {
+ dbParams.sort = {
'connectorID': -1,
'profile.stackLevel': -1,
+ };
}
+ aggregation.push({
+ $sort: dbParams.sort
});
- }
// Skip
aggregation.push({
$skip: dbParams.skip

MODIFY
src/storage/mongodb/CompanyStorage.ts
src/storage/mongodb/CompanyStorage.ts
@@ -160,15 +160,12 @@ export default class CompanyStorage {
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });

MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -199,15 +199,12 @@ export default class LoggingStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { timestamp: -1 }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip

MODIFY
src/storage/mongodb/NotificationStorage.ts
src/storage/mongodb/NotificationStorage.ts
@@ -106,15 +106,12 @@ export default class NotificationStorage {
$unwind: { 'path': '$user', 'preserveNullAndEmptyArrays': true }
});
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { timestamp: -1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/OCPIEndpointStorage.ts
src/storage/mongodb/OCPIEndpointStorage.ts
@@ -146,19 +146,12 @@ export default class OCPIEndpointStorage {
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
- if (dbParams.sort) {
- // Sort
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- // Default
- aggregation.push({
- $sort: {
- name: 1
- }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/OCPPStorage.ts
src/storage/mongodb/OCPPStorage.ts
@@ -72,19 +72,13 @@ export default class OCPPStorage {
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
// Sort
aggregation.push({
$sort: dbParams.sort
});
- } else {
- // Default
- aggregation.push({
- $sort: {
- timestamp: -1
- }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -167,19 +161,12 @@ export default class OCPPStorage {
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Sort
- if (dbParams.sort) {
- // Sort
+ if (!dbParams.sort) {
+ dbParams.sort = { _id: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- // Default
- aggregation.push({
- $sort: {
- _id: 1
- }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip

MODIFY
src/storage/mongodb/RegistrationTokenStorage.ts
src/storage/mongodb/RegistrationTokenStorage.ts
@@ -110,17 +110,12 @@ export default class RegistrationTokenStorage {
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
- if (dbParams.sort) {
- // Sort
+ if (!dbParams.sort) {
+ dbParams.sort = { expirationDate: -1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- // Default
- aggregation.push({
- $sort: { expirationDate: -1 }
- });
- }
// Skip
if (skip > 0) {
aggregation.push({ $skip: skip });

MODIFY
src/storage/mongodb/SettingStorage.ts
src/storage/mongodb/SettingStorage.ts
@@ -329,17 +329,12 @@ export default class SettingStorage {
// Rename ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { identifier: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: {
- identifier: 1
- }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip

MODIFY
src/storage/mongodb/SiteAreaStorage.ts
src/storage/mongodb/SiteAreaStorage.ts
@@ -206,15 +206,12 @@ export default class SiteAreaStorage {
// Remove the limit
aggregation.pop();
// Add Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/SiteStorage.ts
src/storage/mongodb/SiteStorage.ts
@@ -161,15 +161,12 @@ export default class SiteStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { 'user.name': 1, 'user.firstName': 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { 'user.name': 1, 'user.firstName': 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip
@@ -375,15 +372,12 @@ export default class SiteStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { name: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

MODIFY
src/storage/mongodb/TenantStorage.ts
src/storage/mongodb/TenantStorage.ts
@@ -150,20 +150,16 @@ export default class TenantStorage {
}
// Remove the limit
aggregation.pop();
- // Add Created By / Last Changed By
- DatabaseUtils.pushCreatedLastChangedInAggregation('', aggregation);
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { name: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: {
- name: 1
- }
- });
- }
+ console.log('====================================');
+ console.log(dbParams.sort);
+ console.log('====================================');
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -174,6 +170,8 @@ export default class TenantStorage {
});
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
+ // Add Created By / Last Changed By
+ DatabaseUtils.pushCreatedLastChangedInAggregation(Constants.DEFAULT_TENANT, aggregation);
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
@@ -181,9 +179,7 @@ export default class TenantStorage {
.aggregate(aggregation, {
collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 },
allowDiskUse: true
- })
- .toArray();
-
+ }).toArray();
// Debug
Logging.traceEnd(MODULE_NAME, 'getTenants', uniqueTimerID, { params, dbParams });
// Ok

MODIFY
src/storage/mongodb/TransactionStorage.ts
src/storage/mongodb/TransactionStorage.ts
@@ -525,11 +525,10 @@ export default class TransactionStorage {
}
// Remove the limit
aggregation.pop();
- // Not yet possible to remove the fields if stop/remoteStop does not exist (MongoDB 4.2)
- // DatabaseUtils.pushConvertObjectIDToString(aggregation, 'stop.userID');
- // DatabaseUtils.pushConvertObjectIDToString(aggregation, 'remotestop.userID');
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
if (!dbParams.sort.timestamp) {
aggregation.push({
$sort: { ...dbParams.sort, timestamp: -1 }
@@ -539,11 +538,6 @@ export default class TransactionStorage {
$sort: dbParams.sort
});
}
- } else {
- aggregation.push({
- $sort: { timestamp: -1 }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -694,7 +688,9 @@ export default class TransactionStorage {
// DatabaseUtils.pushConvertObjectIDToString(aggregation, 'stop.userID');
// DatabaseUtils.pushConvertObjectIDToString(aggregation, 'remotestop.userID');
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
if (!dbParams.sort.timestamp) {
aggregation.push({
$sort: { ...dbParams.sort, timestamp: -1 }
@@ -704,11 +700,6 @@ export default class TransactionStorage {
$sort: dbParams.sort
});
}
- } else {
- aggregation.push({
- $sort: { timestamp: -1 }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -890,7 +881,9 @@ export default class TransactionStorage {
DatabaseUtils.clearFieldValueIfSubFieldIsNull(aggregation, 'stop', 'timestamp');
DatabaseUtils.clearFieldValueIfSubFieldIsNull(aggregation, 'remotestop', 'timestamp');
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { timestamp: -1 };
+ }
if (!dbParams.sort.timestamp) {
aggregation.push({
$sort: { ...dbParams.sort, timestamp: -1 }
@@ -900,11 +893,6 @@ export default class TransactionStorage {
$sort: dbParams.sort
});
}
- } else {
- aggregation.push({
- $sort: { timestamp: -1 }
- });
- }
// Skip
aggregation.push({
$skip: dbParams.skip

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -651,15 +651,12 @@ export default class UserStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { status: -1, name: 1, firstName: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { status: -1, name: 1, firstName: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip
@@ -769,15 +766,12 @@ export default class UserStorage {
}
// Remove the limit
aggregation.pop();
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { lastChangedOn: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { lastChangedOn: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip
@@ -879,15 +873,12 @@ export default class UserStorage {
// Add Created By / Last Changed By
DatabaseUtils.pushCreatedLastChangedInAggregation(tenantID, aggregation);
// Mongodb sort, skip and limit block
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { status: -1, name: 1, firstName: 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { status: -1, name: 1, firstName: 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip
@@ -996,15 +987,12 @@ export default class UserStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
+ if (!dbParams.sort) {
+ dbParams.sort = { 'site.name': 1 };
+ }
aggregation.push({
$sort: dbParams.sort
});
- } else {
- aggregation.push({
- $sort: { 'site.name': 1 }
- });
- }
// Skip
aggregation.push({
$skip: skip

Reorder the sorting