From eeb92ff1d6f0b790af4e1757aa9d287f809a443f Mon Sep 17 00:00:00 2001 From: Matthis-M-ReliefApps Date: Thu, 17 Jul 2025 11:41:44 +0200 Subject: [PATCH] add unused aids mass deletion script --- .../mongo-db/unused-aids-mass-deletion.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 scripts/guyane/mongo-db/unused-aids-mass-deletion.js diff --git a/scripts/guyane/mongo-db/unused-aids-mass-deletion.js b/scripts/guyane/mongo-db/unused-aids-mass-deletion.js new file mode 100644 index 000000000..f7c52c84a --- /dev/null +++ b/scripts/guyane/mongo-db/unused-aids-mass-deletion.js @@ -0,0 +1,28 @@ +/* eslint-disable prettier/prettier */ + +/* Context: +In the Guyane Alimentaide 9734 project, some orphan aids were created, when there parent prescription was deleted. +This script was built to target and delete those aids. +Once you have tested it enough and want to apply it, simply remove "find" by "deleteMany" in the Step 2. +*/ + +// Step 1: Get all referenced IDs +const allReferencedIds = db.getCollection("records").distinct("data.all_aids"); + +// Step 2: Find records not referenced +const unusedAids = db.getCollection("records").find({ + _id: { $nin: allReferencedIds.map(id => ObjectId(id)) }, + "data.adding_from_family": true, + "data.type": {$ne: "aide nominative - 1ère fois"}, + resource: ObjectId('64e6e0933c7bf3962bf4f04c'), +}); + +// Logging +// eslint-disable-next-line @typescript-eslint/no-unused-expressions +unusedAids.toArray()[unusedAids.count() - 2]._id +console.log("unused aids:") +unusedAids.count() +console.log("total aids:") +db.getCollection("records").find({ + resource: ObjectId('64e6e0933c7bf3962bf4f04c'), +}).count();