-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_replicate.js
44 lines (38 loc) · 1.29 KB
/
index_replicate.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
39
40
41
42
43
44
/*
* Copies the indexes from one instance to another
* WARNING - DO NOT RUN THIS SCRIPT IN PRODUCTION ENVIRONMENT WITH LARGE VOLUMES OF DATA
* Author: Sergio Maruki
*/
hostOrig = '127.0.0.1:27017';
dbNameOrig = 'database1';
hostDest = '127.0.0.1:27017';
dbNameDest = 'database2';
dropIndexesFromDest = false;
removeBackgroundOption = true
dbOrig = new Mongo(hostOrig).getDB(dbNameOrig);
dbDest = new Mongo(hostDest).getDB(dbNameDest);
print("//Copying indexes from "+hostOrig+"/"+dbNameOrig+" to "+hostDest+"/"+dbNameDest)
dbOrig.getCollectionNames().forEach(
function(c){
indexes = dbOrig.getCollection(c).getIndexes();
newIndexes = []
for(x in indexes){
if(indexes[x]['name'] != '_id_'){
delete indexes[x]['v'];
delete indexes[x]['ns'];
if(removeBackgroundOption === true){
delete indexes[x]['background'];
}
newIndexes.push(indexes[x]);
}
}
print('\n//'+c);
if(dropIndexesFromDest === true){
printjson(dbDest.getCollection(c).dropIndexes());
}
if(typeof newIndexes[0] !== 'undefined') {
op = dbDest.runCommand({"createIndexes": c, "indexes": newIndexes});
printjson(op);
}
}
)