Fixed sorting on charging stations
MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -669,7 +669,7 @@ export default class CarStorage {
DatabaseUtils.pushArrayLookupInAggregation('carUsers', DatabaseUtils.pushUserLookupInAggregation.bind(this), {
tenantID, aggregation: aggregation, localField: 'carUsers.userID', foreignField: '_id',
asField: 'carUsers.user', oneToOneCardinality: true, objectIDFields: ['createdBy', 'lastChangedBy']
- }, carUsersPipeline);
+ }, { pipeline: carUsersPipeline });
}
// Add Car Catalog
DatabaseUtils.pushCarCatalogLookupInAggregation({

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -246,17 +246,16 @@ export default class ChargingStationStorage {
// Remove the limit
aggregation.pop();
// Sort
- if (dbParams.sort) {
- aggregation.push({
- $sort: dbParams.sort
- });
- } else {
- aggregation.push({
+ if (!dbParams.sort) {
+ dbParams.sort = {
$sort: {
_id: 1
}
- });
+ };
}
+ aggregation.push({
+ $sort: dbParams.sort
+ });
// Skip
aggregation.push({
$skip: dbParams.skip
@@ -269,7 +268,7 @@ export default class ChargingStationStorage {
DatabaseUtils.pushArrayLookupInAggregation('connectors', DatabaseUtils.pushUserLookupInAggregation.bind(this), {
tenantID, aggregation: aggregation, localField: 'connectors.userID', foreignField: '_id',
asField: 'connectors.user', oneToOneCardinality: true, objectIDFields: ['createdBy', 'lastChangedBy']
- });
+ }, { sort: dbParams.sort });
// Site
if (params.withSite && !params.withNoSiteArea) {
DatabaseUtils.pushSiteLookupInAggregation({

MODIFY
src/storage/mongodb/DatabaseUtils.ts
src/storage/mongodb/DatabaseUtils.ts
@@ -27,7 +27,7 @@ export default class DatabaseUtils {
return `${prefix}.${collectionNameSuffix}`;
}
- public static getNotDeletedFilter(fieldName?: string): object {
+ public static getNotDeletedFilter(fieldName?: string): any {
if (fieldName) {
return JSON.parse(`[
{ "${fieldName}.deleted": { "$exists": false } },
@@ -117,14 +117,14 @@ export default class DatabaseUtils {
public static pushArrayLookupInAggregation(arrayName: string,
lookupMethod: (lookupParams: DbLookup, additionalPipeline?: Record<string, any>[]) => void,
- lookupParams: DbLookup, additionalPipeline?: Record<string, any>[]) {
+ lookupParams: DbLookup, additionalParams: { pipeline?: Record<string, any>[], sort?: any } = {}) {
// Unwind the source
lookupParams.aggregation.push({ '$unwind': { path: `$${arrayName}`, preserveNullAndEmptyArrays: true } });
// Call the lookup
lookupMethod(lookupParams);
// Add external pipeline
- if (!Utils.isEmptyArray(additionalPipeline)) {
- lookupParams.aggregation.push(...additionalPipeline);
+ if (!Utils.isEmptyArray(additionalParams.pipeline)) {
+ lookupParams.aggregation.push(...additionalParams.pipeline);
}
// Group back to arrays
lookupParams.aggregation.push(
@@ -155,6 +155,12 @@ export default class DatabaseUtils {
}`));
// Replace root
lookupParams.aggregation.push({ $replaceRoot: { newRoot: '$root' } });
+ // Sort?
+ if (additionalParams.sort) {
+ lookupParams.aggregation.push({
+ $sort: additionalParams.sort
+ });
+ }
}
public static pushCollectionLookupInAggregation(collection: string, lookupParams: DbLookup, additionalPipeline?: Record<string, any>[]) {

Fixed sorting on charging stations