-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveRecords.js
38 lines (33 loc) · 1018 Bytes
/
RemoveRecords.js
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
34
35
36
37
38
require("dotenv").config();
const mongoose = require("mongoose");
const db = require("./models");
const handleNoArgs = require("./utils/handleNoArgs");
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/datanexus";
const [ collection, queryField, queryValue] = process.argv.filter((item, i) => i > 1)
console.log(collection, queryField, queryValue)
const findAndDelete = (collection, queryField, queryValue) => {
db[collection]
.deleteMany({[queryField] : queryValue})
.then(res => {
console.log('Successful delete', res);
process.exit(0);
})
.catch((err) => {
console.log(err);
process.exit(1);
});}
mongoose
.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("\nConnected to DB.")
collection && queryField && queryValue
? findAndDelete(collection, queryField, queryValue)
: handleNoArgs();
})
.catch((err) => {
console.log(err);
process.exit(1);
});