-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkWrite.js
57 lines (49 loc) · 1.74 KB
/
bulkWrite.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
// ignored first line
const { MongoClient } = require("mongodb");
const fs = require("fs");
const uri =
"mongodb+srv://admin:[email protected]/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
await client.connect();
const mflix = client.db("sample_mflix");
// Replace with the path to your copy of the users.json file
var users = JSON.parse(fs.readFileSync("../../data/users.json"));
var usersToInsert = [];
// Loop through each user in the users array and format the data from
// the JSON file to be a series of insertOne operations. Append
// each appropriately formatted object to the usersToInsert array.
users.forEach(user => {
usersToInsert.push({ insertOne: { document: user } });
});
// Perform the bulk write operation and print out the resulting
// BulkWriteResult object. This is a routine data import
// that does not rely on the ordering of the operations to ensure
// data consistency, so you can set the ordered option to false
// to improve write performance.
mflix
.collection("users")
.bulkWrite(usersToInsert, { ordered: false }, function(error, result) {
if (error) {
console.log("Error: " + error);
} else {
if (result.nInserted > 0) {
console.log("Number of documents inserted: " + result.nInserted);
} else {
console.log(
"No documents were inserted during the bulk write operation",
);
}
}
});
} finally {
console.log("closing");
await client.close();
console.log("closed");
}
}
run().catch(console.dir);