Fixed issue in MongoDB lookup in array
MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -426,10 +426,6 @@ export default class ChargingStationStorage {
// Check Tenant
await Utils.checkTenant(tenantID);
// Build Request
- const chargingStationFilter = {
- _id: chargingStationToSave.id
- };
- // Properties to save
const chargingStationMDB = {
_id: chargingStationToSave.id,
templateHash: chargingStationToSave.templateHash,
@@ -466,7 +462,7 @@ export default class ChargingStationStorage {
chargePoints: chargingStationToSave.chargePoints ? chargingStationToSave.chargePoints.map(
(chargePoint) => ChargingStationStorage.chargePoint2ChargePointMDB(chargePoint)) : [],
coordinates: chargingStationToSave.coordinates,
- remoteAuthorizations: chargingStationToSave.remoteAuthorizations,
+ remoteAuthorizations: chargingStationToSave.remoteAuthorizations ? chargingStationToSave.remoteAuthorizations : [],
currentIPAddress: chargingStationToSave.currentIPAddress,
currentServerLocalIPAddressPort: chargingStationToSave.currentServerLocalIPAddressPort,
capabilities: chargingStationToSave.capabilities,
@@ -474,17 +470,11 @@ export default class ChargingStationStorage {
ocppVendorParameters: chargingStationToSave.ocppVendorParameters,
ocpiData: chargingStationToSave.ocpiData
};
- if (!chargingStationMDB.connectors) {
- chargingStationMDB.connectors = [];
- }
- if (!chargingStationMDB.remoteAuthorizations) {
- chargingStationMDB.remoteAuthorizations = [];
- }
// Add Created/LastChanged By
DatabaseUtils.addLastChangedCreatedProps(chargingStationMDB, chargingStationToSave);
// Modify and return the modified document
await global.database.getCollection<any>(tenantID, 'chargingstations').findOneAndUpdate(
- chargingStationFilter,
+ { _id: chargingStationToSave.id },
{ $set: chargingStationMDB },
{ upsert: true });
// Debug

MODIFY
src/storage/mongodb/DatabaseUtils.ts
src/storage/mongodb/DatabaseUtils.ts
@@ -117,11 +117,9 @@ export default class DatabaseUtils {
public static pushArrayLookupInAggregation(arrayName: string, lookupMethod: (lookupParams: DbLookup) => void, lookupParams: DbLookup) {
// Unwind the source
- lookupParams.aggregation.push({ '$unwind': `$${arrayName}` });
+ lookupParams.aggregation.push({ '$unwind': { path: `$${arrayName}`, preserveNullAndEmptyArrays: true } });
// Call the lookup
lookupMethod(lookupParams);
- // Unwind the result array
- // lookupParams.aggregation.push({ '$unwind': { path: `$${lookupParams.asField}`, preserveNullAndEmptyArrays: true } });
// Group back to arrays
lookupParams.aggregation.push(
JSON.parse(`{
@@ -133,7 +131,7 @@ export default class DatabaseUtils {
}`)
);
// Replace array
- lookupParams.aggregation.push(JSON.parse(`{ "$addFields": { "root.${arrayName}": "$${arrayName}" } }`));
+ lookupParams.aggregation.push(JSON.parse(`{ "$addFields": { "root.${arrayName}": { "$cond": { "if": { "$eq": [ "$connectors", [{}] ] }, "then": [], "else": "$connectors" } } } }`));
// Replace root
lookupParams.aggregation.push({ $replaceRoot: { newRoot: '$root' } });
}

Fixed issue in MongoDB lookup in array