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