-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveFilesFromDb
33 lines (28 loc) · 1.1 KB
/
removeFilesFromDb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require("./access.js");
const fs = require("fs");
const { MongoClient } = require('mongodb');
const { promisify } = require("util");
const writeFile = promisify(fs.writeFile);
const readdir = promisify(fs.readdir);
async function removeFilesFromDb(filePath) {
const client = new MongoClient(database_key, { useUnifiedTopology: true });
await client.connect();
const db = client.db('file_data');
const fileData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const fileNames = fileData.map(file => file.fileName);
const collections = await db.listCollections().toArray();
let coll;
for (const collection of collections) {
coll = db.collection(collection.name);
const bulkOps = [];
for (const fileName of fileNames) {
bulkOps.push({ deleteMany: { filter: { fileName: fileName } } });
}
if (bulkOps.length > 0) {
const result = await coll.bulkWrite(bulkOps);
console.log(`Deleted ${result.deletedCount} file(s) from collection "${collection.name}"`);
}
}
await client.close();
}
removeFilesFromDb('D:\\output\\results.json').catch(error => console.error(error));