-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore.js
More file actions
102 lines (87 loc) · 2.47 KB
/
Copy pathrestore.js
File metadata and controls
102 lines (87 loc) · 2.47 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const mongoose = require('mongoose');
var fs = require('fs');
async function log(arg) {
update(arg)
}
var maxC = 0;
var curC = 1;
async function processCollection(collectionName) {
try {
var collData = data[collectionName];
var coll = db.collection(collectionName);
collData.forEach((cd) => {
cd._id = mongoose.Types.ObjectId(cd._id)
})
await coll.insertMany(collData)
var count = await coll.countDocuments()
log(collectionName, " = ", count);
if(removeDups)
dups(collectionName)
} catch (er) {
log(er.message)
log("error in " + collectionName);
}
log('Processed '+collectionName+"("+(curC+"/"+maxC)+")");
if (curC++ < maxC) {
curC;
}
else {
log("\n\nAll Data restore completed !!");
finish('Restore Complete !')
}
};
async function dups(collectionName) {
var coll = db.collection(collectionName);
await coll.aggregate(
{ "$group": { "_id": "$name", "count": { "$sum": 1 } } },
{ "$match": { "_id": { "$ne": null }, "count": { "$gt": 1 } } },
{ "$project": { "name": "$_id", "_id": 0 } }
)
.toArray((err, results) => {
if (err) throw err;
log("===", results);
})
}
var dumpFile = process.argv[2];
var targetDb = process.argv[3];
var deDupDo = process.argv[4];
var db;
var data;
var update=(msg)=>{
console.log(msg)
}
var finish=(msg)=>{
log(msg)
process.exit()
}
var removeDups=false;
if (dumpFile && targetDb) {
data = JSON.parse(fs.readFileSync(dumpFile));
mongoose.Promise = global.Promise;
mongoose.connect(targetDb, {
useUnifiedTopology: true,
useNewUrlParser: true,
}).then(() => {
log("Successfully connected to the database");
readCollectionsFromDump(mongoose.connection.db,JSON.parse(fs.readFileSync(dumpFile)),deDupDo);
}).catch(err => {
log('Could not connect to the database. Exiting now...', err);
process.exit();
});
}
async function readCollectionsFromDump(connectedDB, dumpData,deDup) {
if(deDup==1){
removeDups=true;
log("Removing duplicates as well")
}
db = connectedDB;
data = (dumpData);
var v = Object.keys(dumpData)
log('Found '+v.length+' Collections to restore');
// log(v);
maxC = v.length;
console.log(v.length)
v.forEach(async function(coll){
await processCollection(coll)
});
}