-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (51 loc) · 1.88 KB
/
app.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
/* eslint-disable handle-callback-err */
const _ = require('lodash')
const MongoClient = require('mongodb').MongoClient
const basePolygon = require('./intersecting-polygon')
// Connection URL
const uri = 'mongodb://localhost:10000/test'
// Database Name
const dbName = 'test'
// How many features we will insert
const featuresToInsert = 10000
const removeAllDocuments = (db, callback) => {
const collection = db.collection('docs')
collection.deleteMany({}, function (err, result) {
callback(result)
})
}
// Use connect method to connect to the server
MongoClient.connect(uri, (err, client) => {
console.log('Connected successfully to server')
const db = client.db(dbName)
const collection = db.collection('docs')
removeAllDocuments(db, () => {
const bulk = collection.initializeUnorderedBulkOp()
// Create 10000 features
for (let i = 0; i < featuresToInsert; ++i) {
// Each feature is an auto-intersecting polygon
const feature = _.clone(basePolygon)
feature.id = i
bulk.insert(feature)
}
bulk.execute((error, result) => {
if (error) {
// Count errors that contains an empty errmsg field
const { true: wellFormedErrors, false: badlyFormedErrors } = _.countBy(error.writeErrors, (error) => {
return error.err.errmsg !== ''
})
console.log(`Errors with errmsg: ${wellFormedErrors}`)
console.log(`Errors with empty errmsg: ${badlyFormedErrors}`)
//
// Prints:
//
// Errors with errmsg: 1391
// Errors with empty errmsg: 8609
//
} else {
console.log(result.result.nInserted === featuresToInsert)
}
client.close()
})
})
})