-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathbulk.js
184 lines (159 loc) · 4.13 KB
/
bulk.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var mongodb = require('mongodb')
var each = require('each-series')
var maxBulkSize = 1000
var noop = function () {}
var oid = mongodb.ObjectID.createPk
var Bulk = function (colName, ordered, onserver, opts) {
if (!opts) { return new Bulk(colName, ordered, onserver, { writeConcern: { w: 1 } }) }
this._colname = colName
this._cmds = []
this._currCmd = null
this._ordered = ordered
this._getConnection = onserver
this._writeConcern = opts.writeConcern
var self = this
this.find = function (query) {
var upsert = false
var findobj = {}
var remove = function (lim) {
if (!self._currCmd) {
self._currCmd = {
delete: self._colname,
deletes: [],
ordered: self._ordered,
writeConcern: self._writeConcern
}
}
if (!self._currCmd.delete || self._currCmd.deletes.length === maxBulkSize) {
self._cmds.push(self._currCmd)
self._currCmd = {
delete: self._colname,
deletes: [],
ordered: self._ordered,
writeConcern: self._writeConcern
}
}
self._currCmd.deletes.push({ q: query, limit: lim })
}
var update = function (updObj, multi) {
if (!self._currCmd) {
self._currCmd = {
update: self._colname,
updates: [],
ordered: self._ordered,
writeConcern: self._writeConcern
}
}
if (!self._currCmd.update || self._currCmd.updates.length === maxBulkSize) {
self._cmds.push(self._currCmd)
self._currCmd = {
update: self._colname,
updates: [],
ordered: self._ordered,
writeConcern: self._writeConcern
}
}
self._currCmd.updates.push({ q: query, u: updObj, multi: multi, upsert: upsert })
}
findobj.upsert = function () {
upsert = true
return findobj
}
findobj.remove = function () {
remove(0)
}
findobj.removeOne = function () {
remove(1)
}
findobj.update = function (updObj) {
update(updObj, true)
}
findobj.updateOne = function (updObj) {
update(updObj, false)
}
findobj.replaceOne = function (updObj) {
this.updateOne(updObj)
}
return findobj
}
}
Bulk.prototype.insert = function (doc) {
if (!this._currCmd) {
this._currCmd = {
insert: this._colname,
documents: [],
ordered: this._ordered,
writeConcern: this._writeConcern
}
}
if (!this._currCmd.insert || this._currCmd.documents.length === maxBulkSize) {
this._cmds.push(this._currCmd)
this._currCmd = {
insert: this._colname,
documents: [],
ordered: this._ordered,
writeConcern: this._writeConcern
}
}
if (!doc._id) doc._id = oid()
this._currCmd.documents.push(doc)
}
var cmdkeys = {
insert: 'nInserted',
delete: 'nRemoved',
update: 'nUpserted'
}
Bulk.prototype.tojson = function () {
if (this._currCmd) this._cmds.push(this._currCmd)
var obj = {
nInsertOps: 0,
nUpdateOps: 0,
nRemoveOps: 0,
nBatches: this._cmds.length
}
this._cmds.forEach(function (cmd) {
if (cmd.update) {
obj.nUpdateOps += cmd.updates.length
} else if (cmd.insert) {
obj.nInsertOps += cmd.documents.length
} else if (cmd.delete) {
obj.nRemoveOps += cmd.deletes.length
}
})
return obj
}
Bulk.prototype.toString = function () {
return JSON.stringify(this.tojson())
}
Bulk.prototype.execute = function (cb) {
if (!cb) return this.execute(noop)
var self = this
var result = {
writeErrors: [],
writeConcernErrors: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
}
if (this._currCmd) {
this._cmds.push(this._currCmd)
}
this._getConnection(function (err, connection) {
if (err) return cb(err)
each(self._cmds, function (cmd, i, done) {
connection.command(cmd, function (err, res) {
if (err) return done(err)
result[cmdkeys[Object.keys(cmd)[0]]] += res.n
done()
})
}, function (err) {
if (err) return cb(err)
result.ok = 1
cb(null, result)
})
})
}
module.exports = Bulk