-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrateDb.js
134 lines (109 loc) · 2.24 KB
/
migrateDb.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var gogogo2 = function(toCollection, modifyLive)
{
db[toCollection].drop();
db.createCollection(toCollection);
// Clone collection
db.docs.find().forEach( function(x){db[toCollection].insert(x)} );
var currentCollection = modifyLive ? "docs" : toCollection;
var cursor = db[currentCollection].find();
var migrate = function(doc)
{
var ps = doc.data.payments;
var newPs = [];
// for each payment
for (var i = 0; i < ps.length; i++)
{
var vs = ps[i].values;
var newVs = [];
for (var j = 0; j < vs.length; j++)
{
var v = vs[j];
newVs.push([v[0], Math.abs(v[1])]);
}
ps[i].values = newVs;
}
return doc;
};
while (cursor.hasNext())
{
var doc = migrate(cursor.next());
if (!doc)
{
print("No doc!");
return;
}
db[currentCollection].save(doc);
}
};
var gogogo = function(toCollection, modifyLive)
{
db[toCollection].drop();
db.createCollection(toCollection);
// Clone collection
db.docs.find().forEach( function(x){db[toCollection].insert(x)} );
var currentCollection = modifyLive ? "docs" : toCollection;
var cursor = db[currentCollection].find();
var migrate = function(doc)
{
var ps = doc.data.payments;
var newPs = [];
// for each payment
for (var i = 0; i < ps.length; i++)
{
var vs = ps[i].values;
var newVs = [];
var sum = 0;
var count = 0;
// values sum/count
for (var j = 0; j < vs.length; j++)
{
if (vs[j] != null)
{
sum += vs[j];
count++;
}
}
var average = sum/count;
for (var j = 0; j < vs.length; j++)
{
var v = vs[j];
var plus = 0;
var minus = 0;
if (v != null)
{
if (v >= 0)
{
plus = v;
minus = -average;
}
else
{
if (average > 0.001 || average < -0.001)
{
print("Fail!!!");
return false;
}
plus = 0;
minus = -average;
}
}
newVs.push([plus, minus]);
}
ps[i].values = newVs;
//printjson(newVs);
}
//printjson(doc.data.payments);
//print("-------------");
return doc;
};
while (cursor.hasNext())
{
var doc = migrate(cursor.next());
if (!doc)
{
print("No doc!");
return;
}
db[currentCollection].save(doc);
}
};