forked from jscomplete/learn-fullstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateTestData.js
32 lines (28 loc) · 854 Bytes
/
updateTestData.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
import { MongoClient } from 'mongodb';
import assert from 'assert';
import config from './config';
MongoClient.connect(config.mongodbUri, (err, db) => {
assert.equal(null, err);
let contestCount = 0;
db.collection('contests').find({}).each((err, contest) => {
assert.equal(null, err);
if (!contest) { return; }
contestCount++;
db.collection('names')
.find({ id: { $in: contest.nameIds }})
.project({ _id: 1 })
.toArray()
.then(_ids => {
const newIds = _ids.map(o => o._id);
db.collection('contests').updateOne(
{ id: contest.id },
{ $set: { nameIds: newIds } }
).then(() => {
console.info('Updated', contest._id);
contestCount--;
if (contestCount === 0) { db.close(); }
});
})
.catch(console.error);
});
});