-
Notifications
You must be signed in to change notification settings - Fork 13
/
update-count.js
48 lines (43 loc) · 1.82 KB
/
update-count.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
var model = require('./domain/model.js');
var mongoose = require('mongoose');
var fs = require('fs');
//Initialize Mongoose
//mongodb://af_brew-o-matic-lautaromail:[email protected]:47948/af_brew-o-matic-lautaromail
mongoose.connect(process.env.MONGOLAB_URI);
//mongoose.connect('mongodb://app:[email protected]:47948/af_brew-o-matic-lautaromail');
// model.Recipe.find({},{NAME:1,starredBy:1,starredByCount:1}).exec(function(err,results) {
// results.forEach(function(r) {
// var count = r.starredBy ? r.starredBy.length : 0;
// console.log('Updating ' + r.NAME + ' with ' + count);
// });
// // console.log('ERR', err);
// // console.log('COUNT', results.length);
// mongoose.disconnect();
// });
var saved = 0;
model.Recipe.find({}, {NAME:1,clonedBy:1,clonedByCount:1}).exec(function(err,results) {
saved = results.length;
results.forEach(function(r) {
var count = r.clonedBy ? r.clonedBy.length : 0;
console.log('Updating ' + JSON.stringify(r) + ' with ' + count);
r.clonedByCount = count;
r.save(function(err) {
if ( err ) console.log('ERR', err);
saved--;
console.log('REST', saved);
if ( saved === 0 ) {
mongoose.disconnect();
}
})
});
// console.log(JSON.stringify(results[0],null,4));
// // var count = results[0].starredBy ? results[0].starredBy.length : 0;
// // console.log('Updating ' + results[0].NAME + ' with ' + count);
// // results[0].starredByCount = count;
// // results[0].save(function(err) {
// // console.log(err);mongoose.disconnect();
// // });
// // console.log('ERR', err);
// // console.log('COUNT', results.length);
// // mongoose.disconnect();
});