| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | |
| 5 | const { MongoClient } = require('mongodb'); |
| 6 | |
| 7 | // This script deletes charging stations |
| 8 | // Filter charging stations by id pattern |
| 9 | |
| 10 | // Use Case: e-mobility-charging-stations-simulator creates thousands of charging stations, which are not longer needed. |
| 11 | // Delete these charging stations all at once |
| 12 | |
| 13 | // Config |
| 14 | const config = JSON.parse(fs.readFileSync('scriptConfig.json', 'utf8')); |
| 15 | |
| 16 | // Mongo Connection and Query |
| 17 | if (config && config.mongoConnectionString) { |
| 18 | MongoClient.connect(config.mongoConnectionString, async function (err, client) { |
| 19 | const db = client.db(); |
| 20 | |
| 21 | for await (const tenantID of config.tenantIDs) { |
| 22 | const response = await db |
| 23 | .collection(`${tenantID}.chargingstations`) |
| 24 | .deleteMany({ _id: { $regex: config.idPattern } }); |
| 25 | console.info( |
| 26 | response.deletedCount, |
| 27 | `Charging Stations with id = %${config.idPattern}% deleted. TenantID =`, |
| 28 | tenantID, |
| 29 | ); |
| 30 | } |
| 31 | client.close(); |
| 32 | }); |
| 33 | } |