-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
84 lines (78 loc) · 2.21 KB
/
example.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
'use strict';
/**
* @description
* DOING: Should import or include the required
* libraries and files for this script to run.
*/
const { MongoClient } = require("mongodb");
const utils = require('./');
async function run() {
try {
// Connect to MongoDB
const uri = "mongodb://127.0.0.1:27017/meteor";
const client = new MongoClient(uri, {
poolSize: 2,
bufferMaxEntries: 0,
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.connect();
const database = client.db('meteor');
const contracts = database.collection('contracts');
// Tell mongo-utils what to do
const updateAddOns = utils({
handler: 'migrate',
params: {
connection: {
client,
database,
collection: contracts
},
cursor: {
fields: ['addOns.id'],
limit: 100
},
path: ['addOns'],
condition: { $exists: true },
data: {
newObj: {
stringValue: 'string',
numericValuee: 10,
listValue: [0, 1, 2, 3],
objectValue: { key: 'value' }
},
newList: [[0, 1, 2, 3], [0, 1, 2, 3]],
newString: 'test string',
newNumber: 1001011,
newBool: true,
newRandom: '>>RANDOM',
newCopy: '$$id'
},
options: {
verbose: true,
writeMode: false, // set true for real update
document: {
upsert: true,
multi: true,
arrayFilters: [
{ 'addOns.id': { $exists: true } },
{ 'addOns.newObj': { $exists: false } },
{ 'addOns.newList': { $exists: false } },
{ 'addOns.newString': { $exists: false } },
{ 'addOns.newNumber': { $exists: false } },
{ 'addOns.newBool': { $exists: false } },
{ 'addOns.newRandom': { $exists: false } },
{ 'addOns.newCopy': { $exists: false } }
]
}
}
}
});
// Execute mongo-utils
const result = await updateAddOns();
console.log('Result from database: ', result); //eslint-disable-line
} catch (e) {
throw new Error(e);
}
};
run();