mongodb 2.2.36 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +24 -10
- package/CHANGES_3.0.0.md +288 -0
- package/HISTORY.md +120 -25
- package/README.md +247 -176
- package/conf.json +15 -14
- package/index.js +12 -6
- package/lib/admin.js +156 -364
- package/lib/aggregation_cursor.js +86 -88
- package/lib/apm.js +192 -149
- package/lib/authenticate.js +73 -53
- package/lib/bulk/common.js +140 -115
- package/lib/bulk/ordered.js +273 -195
- package/lib/bulk/unordered.js +291 -201
- package/lib/change_stream.js +359 -0
- package/lib/collection.js +1128 -1368
- package/lib/command_cursor.js +100 -73
- package/lib/cursor.js +454 -358
- package/lib/db.js +775 -771
- package/lib/gridfs/chunk.js +38 -34
- package/lib/gridfs/grid_store.js +590 -593
- package/lib/gridfs-stream/download.js +46 -40
- package/lib/gridfs-stream/index.js +24 -45
- package/lib/gridfs-stream/upload.js +28 -26
- package/lib/metadata.js +22 -16
- package/lib/mongo_client.js +708 -285
- package/lib/topologies/mongos.js +444 -0
- package/lib/topologies/replset.js +501 -0
- package/lib/topologies/server.js +448 -0
- package/lib/topologies/topology_base.js +441 -0
- package/lib/url_parser.js +334 -257
- package/lib/utils.js +210 -132
- package/package.json +20 -32
- package/yarn.lock +3728 -0
- package/lib/mongos.js +0 -533
- package/lib/read_preference.js +0 -131
- package/lib/replset.js +0 -582
- package/lib/server.js +0 -518
- package/lib/topology_base.js +0 -191
package/lib/bulk/common.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
var Long = require('mongodb-core').BSON.Long,
|
|
4
|
-
|
|
4
|
+
MongoError = require('mongodb-core').MongoError,
|
|
5
|
+
util = require('util');
|
|
5
6
|
|
|
6
7
|
// Error codes
|
|
7
8
|
var UNKNOWN_ERROR = 8;
|
|
@@ -12,28 +13,29 @@ var MULTIPLE_ERROR = 65;
|
|
|
12
13
|
// Insert types
|
|
13
14
|
var INSERT = 1;
|
|
14
15
|
var UPDATE = 2;
|
|
15
|
-
var REMOVE = 3
|
|
16
|
-
|
|
16
|
+
var REMOVE = 3;
|
|
17
17
|
|
|
18
18
|
// Get write concern
|
|
19
19
|
var writeConcern = function(target, col, options) {
|
|
20
20
|
var writeConcern = {};
|
|
21
21
|
|
|
22
22
|
// Collection level write concern
|
|
23
|
-
if(col.writeConcern && col.writeConcern.w != null) writeConcern.w = col.writeConcern.w;
|
|
24
|
-
if(col.writeConcern && col.writeConcern.j != null) writeConcern.j = col.writeConcern.j;
|
|
25
|
-
if(col.writeConcern && col.writeConcern.fsync != null)
|
|
26
|
-
|
|
23
|
+
if (col.writeConcern && col.writeConcern.w != null) writeConcern.w = col.writeConcern.w;
|
|
24
|
+
if (col.writeConcern && col.writeConcern.j != null) writeConcern.j = col.writeConcern.j;
|
|
25
|
+
if (col.writeConcern && col.writeConcern.fsync != null)
|
|
26
|
+
writeConcern.fsync = col.writeConcern.fsync;
|
|
27
|
+
if (col.writeConcern && col.writeConcern.wtimeout != null)
|
|
28
|
+
writeConcern.wtimeout = col.writeConcern.wtimeout;
|
|
27
29
|
|
|
28
30
|
// Options level write concern
|
|
29
|
-
if(options && options.w != null) writeConcern.w = options.w;
|
|
30
|
-
if(options && options.wtimeout != null) writeConcern.wtimeout = options.wtimeout;
|
|
31
|
-
if(options && options.j != null) writeConcern.j = options.j;
|
|
32
|
-
if(options && options.fsync != null) writeConcern.fsync = options.fsync;
|
|
31
|
+
if (options && options.w != null) writeConcern.w = options.w;
|
|
32
|
+
if (options && options.wtimeout != null) writeConcern.wtimeout = options.wtimeout;
|
|
33
|
+
if (options && options.j != null) writeConcern.j = options.j;
|
|
34
|
+
if (options && options.fsync != null) writeConcern.fsync = options.fsync;
|
|
33
35
|
|
|
34
36
|
// Return write concern
|
|
35
37
|
return writeConcern;
|
|
36
|
-
}
|
|
38
|
+
};
|
|
37
39
|
|
|
38
40
|
/**
|
|
39
41
|
* Helper function to define properties
|
|
@@ -41,12 +43,12 @@ var writeConcern = function(target, col, options) {
|
|
|
41
43
|
*/
|
|
42
44
|
var defineReadOnlyProperty = function(self, name, value) {
|
|
43
45
|
Object.defineProperty(self, name, {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get: function() {
|
|
46
48
|
return value;
|
|
47
49
|
}
|
|
48
50
|
});
|
|
49
|
-
}
|
|
51
|
+
};
|
|
50
52
|
|
|
51
53
|
/**
|
|
52
54
|
* Keeps the state of a unordered batch so we can rewrite the results
|
|
@@ -61,7 +63,7 @@ var Batch = function(batchType, originalZeroIndex) {
|
|
|
61
63
|
this.operations = [];
|
|
62
64
|
this.size = 0;
|
|
63
65
|
this.sizeBytes = 0;
|
|
64
|
-
}
|
|
66
|
+
};
|
|
65
67
|
|
|
66
68
|
/**
|
|
67
69
|
* Wraps a legacy operation so we can correctly rewrite it's error
|
|
@@ -71,7 +73,7 @@ var LegacyOp = function(batchType, operation, index) {
|
|
|
71
73
|
this.batchType = batchType;
|
|
72
74
|
this.index = index;
|
|
73
75
|
this.operation = operation;
|
|
74
|
-
}
|
|
76
|
+
};
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
79
|
* Create a new BulkWriteResult instance (INTERNAL TYPE, do not instantiate directly)
|
|
@@ -86,12 +88,12 @@ var LegacyOp = function(batchType, operation, index) {
|
|
|
86
88
|
* @return {BulkWriteResult} a BulkWriteResult instance
|
|
87
89
|
*/
|
|
88
90
|
var BulkWriteResult = function(bulkResult) {
|
|
89
|
-
defineReadOnlyProperty(this,
|
|
90
|
-
defineReadOnlyProperty(this,
|
|
91
|
-
defineReadOnlyProperty(this,
|
|
92
|
-
defineReadOnlyProperty(this,
|
|
93
|
-
defineReadOnlyProperty(this,
|
|
94
|
-
defineReadOnlyProperty(this,
|
|
91
|
+
defineReadOnlyProperty(this, 'ok', bulkResult.ok);
|
|
92
|
+
defineReadOnlyProperty(this, 'nInserted', bulkResult.nInserted);
|
|
93
|
+
defineReadOnlyProperty(this, 'nUpserted', bulkResult.nUpserted);
|
|
94
|
+
defineReadOnlyProperty(this, 'nMatched', bulkResult.nMatched);
|
|
95
|
+
defineReadOnlyProperty(this, 'nModified', bulkResult.nModified);
|
|
96
|
+
defineReadOnlyProperty(this, 'nRemoved', bulkResult.nRemoved);
|
|
95
97
|
|
|
96
98
|
/**
|
|
97
99
|
* Return an array of inserted ids
|
|
@@ -100,7 +102,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
100
102
|
*/
|
|
101
103
|
this.getInsertedIds = function() {
|
|
102
104
|
return bulkResult.insertedIds;
|
|
103
|
-
}
|
|
105
|
+
};
|
|
104
106
|
|
|
105
107
|
/**
|
|
106
108
|
* Return an array of upserted ids
|
|
@@ -109,7 +111,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
109
111
|
*/
|
|
110
112
|
this.getUpsertedIds = function() {
|
|
111
113
|
return bulkResult.upserted;
|
|
112
|
-
}
|
|
114
|
+
};
|
|
113
115
|
|
|
114
116
|
/**
|
|
115
117
|
* Return the upserted id at position x
|
|
@@ -119,7 +121,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
119
121
|
*/
|
|
120
122
|
this.getUpsertedIdAt = function(index) {
|
|
121
123
|
return bulkResult.upserted[index];
|
|
122
|
-
}
|
|
124
|
+
};
|
|
123
125
|
|
|
124
126
|
/**
|
|
125
127
|
* Return raw internal result
|
|
@@ -128,7 +130,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
128
130
|
*/
|
|
129
131
|
this.getRawResponse = function() {
|
|
130
132
|
return bulkResult;
|
|
131
|
-
}
|
|
133
|
+
};
|
|
132
134
|
|
|
133
135
|
/**
|
|
134
136
|
* Returns true if the bulk operation contains a write error
|
|
@@ -137,7 +139,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
137
139
|
*/
|
|
138
140
|
this.hasWriteErrors = function() {
|
|
139
141
|
return bulkResult.writeErrors.length > 0;
|
|
140
|
-
}
|
|
142
|
+
};
|
|
141
143
|
|
|
142
144
|
/**
|
|
143
145
|
* Returns the number of write errors off the bulk operation
|
|
@@ -146,7 +148,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
146
148
|
*/
|
|
147
149
|
this.getWriteErrorCount = function() {
|
|
148
150
|
return bulkResult.writeErrors.length;
|
|
149
|
-
}
|
|
151
|
+
};
|
|
150
152
|
|
|
151
153
|
/**
|
|
152
154
|
* Returns a specific write error object
|
|
@@ -155,11 +157,11 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
155
157
|
* @return {WriteError}
|
|
156
158
|
*/
|
|
157
159
|
this.getWriteErrorAt = function(index) {
|
|
158
|
-
if(index < bulkResult.writeErrors.length) {
|
|
160
|
+
if (index < bulkResult.writeErrors.length) {
|
|
159
161
|
return bulkResult.writeErrors[index];
|
|
160
162
|
}
|
|
161
163
|
return null;
|
|
162
|
-
}
|
|
164
|
+
};
|
|
163
165
|
|
|
164
166
|
/**
|
|
165
167
|
* Retrieve all write errors
|
|
@@ -168,7 +170,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
168
170
|
*/
|
|
169
171
|
this.getWriteErrors = function() {
|
|
170
172
|
return bulkResult.writeErrors;
|
|
171
|
-
}
|
|
173
|
+
};
|
|
172
174
|
|
|
173
175
|
/**
|
|
174
176
|
* Retrieve lastOp if available
|
|
@@ -177,7 +179,7 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
177
179
|
*/
|
|
178
180
|
this.getLastOp = function() {
|
|
179
181
|
return bulkResult.lastOp;
|
|
180
|
-
}
|
|
182
|
+
};
|
|
181
183
|
|
|
182
184
|
/**
|
|
183
185
|
* Retrieve the write concern error if any
|
|
@@ -185,39 +187,38 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
185
187
|
* @return {WriteConcernError}
|
|
186
188
|
*/
|
|
187
189
|
this.getWriteConcernError = function() {
|
|
188
|
-
if(bulkResult.writeConcernErrors.length
|
|
190
|
+
if (bulkResult.writeConcernErrors.length === 0) {
|
|
189
191
|
return null;
|
|
190
|
-
} else if(bulkResult.writeConcernErrors.length
|
|
192
|
+
} else if (bulkResult.writeConcernErrors.length === 1) {
|
|
191
193
|
// Return the error
|
|
192
194
|
return bulkResult.writeConcernErrors[0];
|
|
193
195
|
} else {
|
|
194
|
-
|
|
195
196
|
// Combine the errors
|
|
196
|
-
var errmsg =
|
|
197
|
-
for(var i = 0; i < bulkResult.writeConcernErrors.length; i++) {
|
|
197
|
+
var errmsg = '';
|
|
198
|
+
for (var i = 0; i < bulkResult.writeConcernErrors.length; i++) {
|
|
198
199
|
var err = bulkResult.writeConcernErrors[i];
|
|
199
200
|
errmsg = errmsg + err.errmsg;
|
|
200
201
|
|
|
201
202
|
// TODO: Something better
|
|
202
|
-
if(i
|
|
203
|
+
if (i === 0) errmsg = errmsg + ' and ';
|
|
203
204
|
}
|
|
204
205
|
|
|
205
|
-
return new WriteConcernError({ errmsg
|
|
206
|
+
return new WriteConcernError({ errmsg: errmsg, code: WRITE_CONCERN_ERROR });
|
|
206
207
|
}
|
|
207
|
-
}
|
|
208
|
+
};
|
|
208
209
|
|
|
209
210
|
this.toJSON = function() {
|
|
210
211
|
return bulkResult;
|
|
211
|
-
}
|
|
212
|
+
};
|
|
212
213
|
|
|
213
214
|
this.toString = function() {
|
|
214
|
-
return
|
|
215
|
-
}
|
|
215
|
+
return 'BulkWriteResult(' + this.toJSON(bulkResult) + ')';
|
|
216
|
+
};
|
|
216
217
|
|
|
217
218
|
this.isOk = function() {
|
|
218
|
-
return bulkResult.ok
|
|
219
|
-
}
|
|
220
|
-
}
|
|
219
|
+
return bulkResult.ok === 1;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
221
222
|
|
|
222
223
|
/**
|
|
223
224
|
* Create a new WriteConcernError instance (INTERNAL TYPE, do not instantiate directly)
|
|
@@ -228,20 +229,20 @@ var BulkWriteResult = function(bulkResult) {
|
|
|
228
229
|
* @return {WriteConcernError} a WriteConcernError instance
|
|
229
230
|
*/
|
|
230
231
|
var WriteConcernError = function(err) {
|
|
231
|
-
if(!(this instanceof WriteConcernError)) return new WriteConcernError(err);
|
|
232
|
+
if (!(this instanceof WriteConcernError)) return new WriteConcernError(err);
|
|
232
233
|
|
|
233
234
|
// Define properties
|
|
234
|
-
defineReadOnlyProperty(this,
|
|
235
|
-
defineReadOnlyProperty(this,
|
|
235
|
+
defineReadOnlyProperty(this, 'code', err.code);
|
|
236
|
+
defineReadOnlyProperty(this, 'errmsg', err.errmsg);
|
|
236
237
|
|
|
237
238
|
this.toJSON = function() {
|
|
238
|
-
return {code: err.code, errmsg: err.errmsg};
|
|
239
|
-
}
|
|
239
|
+
return { code: err.code, errmsg: err.errmsg };
|
|
240
|
+
};
|
|
240
241
|
|
|
241
242
|
this.toString = function() {
|
|
242
|
-
return
|
|
243
|
-
}
|
|
244
|
-
}
|
|
243
|
+
return 'WriteConcernError(' + err.errmsg + ')';
|
|
244
|
+
};
|
|
245
|
+
};
|
|
245
246
|
|
|
246
247
|
/**
|
|
247
248
|
* Create a new WriteError instance (INTERNAL TYPE, do not instantiate directly)
|
|
@@ -253,27 +254,27 @@ var WriteConcernError = function(err) {
|
|
|
253
254
|
* @return {WriteConcernError} a WriteConcernError instance
|
|
254
255
|
*/
|
|
255
256
|
var WriteError = function(err) {
|
|
256
|
-
if(!(this instanceof WriteError)) return new WriteError(err);
|
|
257
|
+
if (!(this instanceof WriteError)) return new WriteError(err);
|
|
257
258
|
|
|
258
259
|
// Define properties
|
|
259
|
-
defineReadOnlyProperty(this,
|
|
260
|
-
defineReadOnlyProperty(this,
|
|
261
|
-
defineReadOnlyProperty(this,
|
|
260
|
+
defineReadOnlyProperty(this, 'code', err.code);
|
|
261
|
+
defineReadOnlyProperty(this, 'index', err.index);
|
|
262
|
+
defineReadOnlyProperty(this, 'errmsg', err.errmsg);
|
|
262
263
|
|
|
263
264
|
//
|
|
264
265
|
// Define access methods
|
|
265
266
|
this.getOperation = function() {
|
|
266
267
|
return err.op;
|
|
267
|
-
}
|
|
268
|
+
};
|
|
268
269
|
|
|
269
270
|
this.toJSON = function() {
|
|
270
|
-
return {code: err.code, index: err.index, errmsg: err.errmsg, op: err.op};
|
|
271
|
-
}
|
|
271
|
+
return { code: err.code, index: err.index, errmsg: err.errmsg, op: err.op };
|
|
272
|
+
};
|
|
272
273
|
|
|
273
274
|
this.toString = function() {
|
|
274
|
-
return
|
|
275
|
-
}
|
|
276
|
-
}
|
|
275
|
+
return 'WriteError(' + JSON.stringify(this.toJSON()) + ')';
|
|
276
|
+
};
|
|
277
|
+
};
|
|
277
278
|
|
|
278
279
|
/**
|
|
279
280
|
* Merges results into shared data structure
|
|
@@ -281,66 +282,68 @@ var WriteError = function(err) {
|
|
|
281
282
|
*/
|
|
282
283
|
var mergeBatchResults = function(ordered, batch, bulkResult, err, result) {
|
|
283
284
|
// If we have an error set the result to be the err object
|
|
284
|
-
if(err) {
|
|
285
|
+
if (err) {
|
|
285
286
|
result = err;
|
|
286
|
-
} else if(result && result.result) {
|
|
287
|
+
} else if (result && result.result) {
|
|
287
288
|
result = result.result;
|
|
288
|
-
} else if(result == null) {
|
|
289
|
+
} else if (result == null) {
|
|
289
290
|
return;
|
|
290
291
|
}
|
|
291
292
|
|
|
292
293
|
// Do we have a top level error stop processing and return
|
|
293
|
-
if(result.ok
|
|
294
|
+
if (result.ok === 0 && bulkResult.ok === 1) {
|
|
294
295
|
bulkResult.ok = 0;
|
|
295
296
|
|
|
296
297
|
var writeError = {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
298
|
+
index: 0,
|
|
299
|
+
code: result.code || 0,
|
|
300
|
+
errmsg: result.message,
|
|
301
|
+
op: batch.operations[0]
|
|
301
302
|
};
|
|
302
303
|
|
|
303
304
|
bulkResult.writeErrors.push(new WriteError(writeError));
|
|
304
305
|
return;
|
|
305
|
-
} else if(result.ok
|
|
306
|
+
} else if (result.ok === 0 && bulkResult.ok === 0) {
|
|
306
307
|
return;
|
|
307
308
|
}
|
|
308
309
|
|
|
309
310
|
// Deal with opTime if available
|
|
310
|
-
if(result.opTime || result.lastOp) {
|
|
311
|
+
if (result.opTime || result.lastOp) {
|
|
311
312
|
var opTime = result.lastOp || result.opTime;
|
|
312
313
|
var lastOpTS = null;
|
|
313
314
|
var lastOpT = null;
|
|
314
315
|
|
|
315
316
|
// We have a time stamp
|
|
316
|
-
if(opTime && opTime._bsontype
|
|
317
|
-
if(bulkResult.lastOp == null) {
|
|
317
|
+
if (opTime && opTime._bsontype === 'Timestamp') {
|
|
318
|
+
if (bulkResult.lastOp == null) {
|
|
318
319
|
bulkResult.lastOp = opTime;
|
|
319
|
-
} else if(opTime.greaterThan(bulkResult.lastOp)) {
|
|
320
|
+
} else if (opTime.greaterThan(bulkResult.lastOp)) {
|
|
320
321
|
bulkResult.lastOp = opTime;
|
|
321
322
|
}
|
|
322
323
|
} else {
|
|
323
324
|
// Existing TS
|
|
324
|
-
if(bulkResult.lastOp) {
|
|
325
|
-
lastOpTS =
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
325
|
+
if (bulkResult.lastOp) {
|
|
326
|
+
lastOpTS =
|
|
327
|
+
typeof bulkResult.lastOp.ts === 'number'
|
|
328
|
+
? Long.fromNumber(bulkResult.lastOp.ts)
|
|
329
|
+
: bulkResult.lastOp.ts;
|
|
330
|
+
lastOpT =
|
|
331
|
+
typeof bulkResult.lastOp.t === 'number'
|
|
332
|
+
? Long.fromNumber(bulkResult.lastOp.t)
|
|
333
|
+
: bulkResult.lastOp.t;
|
|
329
334
|
}
|
|
330
335
|
|
|
331
336
|
// Current OpTime TS
|
|
332
|
-
var opTimeTS = typeof opTime.ts
|
|
333
|
-
|
|
334
|
-
var opTimeT = typeof opTime.t == 'number'
|
|
335
|
-
? Long.fromNumber(opTime.t) : opTime.t;
|
|
337
|
+
var opTimeTS = typeof opTime.ts === 'number' ? Long.fromNumber(opTime.ts) : opTime.ts;
|
|
338
|
+
var opTimeT = typeof opTime.t === 'number' ? Long.fromNumber(opTime.t) : opTime.t;
|
|
336
339
|
|
|
337
340
|
// Compare the opTime's
|
|
338
|
-
if(bulkResult.lastOp == null) {
|
|
341
|
+
if (bulkResult.lastOp == null) {
|
|
339
342
|
bulkResult.lastOp = opTime;
|
|
340
|
-
} else if(opTimeTS.greaterThan(lastOpTS)) {
|
|
343
|
+
} else if (opTimeTS.greaterThan(lastOpTS)) {
|
|
341
344
|
bulkResult.lastOp = opTime;
|
|
342
|
-
} else if(opTimeTS.equals(lastOpTS)) {
|
|
343
|
-
if(opTimeT.greaterThan(lastOpT)) {
|
|
345
|
+
} else if (opTimeTS.equals(lastOpTS)) {
|
|
346
|
+
if (opTimeT.greaterThan(lastOpT)) {
|
|
344
347
|
bulkResult.lastOp = opTime;
|
|
345
348
|
}
|
|
346
349
|
}
|
|
@@ -348,82 +351,104 @@ var mergeBatchResults = function(ordered, batch, bulkResult, err, result) {
|
|
|
348
351
|
}
|
|
349
352
|
|
|
350
353
|
// If we have an insert Batch type
|
|
351
|
-
if(batch.batchType
|
|
354
|
+
if (batch.batchType === INSERT && result.n) {
|
|
352
355
|
bulkResult.nInserted = bulkResult.nInserted + result.n;
|
|
353
356
|
}
|
|
354
357
|
|
|
355
358
|
// If we have an insert Batch type
|
|
356
|
-
if(batch.batchType
|
|
359
|
+
if (batch.batchType === REMOVE && result.n) {
|
|
357
360
|
bulkResult.nRemoved = bulkResult.nRemoved + result.n;
|
|
358
361
|
}
|
|
359
362
|
|
|
360
363
|
var nUpserted = 0;
|
|
361
364
|
|
|
362
365
|
// We have an array of upserted values, we need to rewrite the indexes
|
|
363
|
-
if(Array.isArray(result.upserted)) {
|
|
366
|
+
if (Array.isArray(result.upserted)) {
|
|
364
367
|
nUpserted = result.upserted.length;
|
|
365
368
|
|
|
366
|
-
for(var i = 0; i < result.upserted.length; i++) {
|
|
369
|
+
for (var i = 0; i < result.upserted.length; i++) {
|
|
367
370
|
bulkResult.upserted.push({
|
|
368
|
-
|
|
369
|
-
|
|
371
|
+
index: result.upserted[i].index + batch.originalZeroIndex,
|
|
372
|
+
_id: result.upserted[i]._id
|
|
370
373
|
});
|
|
371
374
|
}
|
|
372
|
-
} else if(result.upserted) {
|
|
373
|
-
|
|
375
|
+
} else if (result.upserted) {
|
|
374
376
|
nUpserted = 1;
|
|
375
377
|
|
|
376
378
|
bulkResult.upserted.push({
|
|
377
|
-
|
|
378
|
-
|
|
379
|
+
index: batch.originalZeroIndex,
|
|
380
|
+
_id: result.upserted
|
|
379
381
|
});
|
|
380
382
|
}
|
|
381
383
|
|
|
382
384
|
// If we have an update Batch type
|
|
383
|
-
if(batch.batchType
|
|
385
|
+
if (batch.batchType === UPDATE && result.n) {
|
|
384
386
|
var nModified = result.nModified;
|
|
385
387
|
bulkResult.nUpserted = bulkResult.nUpserted + nUpserted;
|
|
386
388
|
bulkResult.nMatched = bulkResult.nMatched + (result.n - nUpserted);
|
|
387
389
|
|
|
388
|
-
if(typeof nModified
|
|
390
|
+
if (typeof nModified === 'number') {
|
|
389
391
|
bulkResult.nModified = bulkResult.nModified + nModified;
|
|
390
392
|
} else {
|
|
391
393
|
bulkResult.nModified = null;
|
|
392
394
|
}
|
|
393
395
|
}
|
|
394
396
|
|
|
395
|
-
if(Array.isArray(result.writeErrors)) {
|
|
396
|
-
for(i = 0; i < result.writeErrors.length; i++) {
|
|
397
|
-
|
|
397
|
+
if (Array.isArray(result.writeErrors)) {
|
|
398
|
+
for (i = 0; i < result.writeErrors.length; i++) {
|
|
398
399
|
writeError = {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
400
|
+
index: batch.originalZeroIndex + result.writeErrors[i].index,
|
|
401
|
+
code: result.writeErrors[i].code,
|
|
402
|
+
errmsg: result.writeErrors[i].errmsg,
|
|
403
|
+
op: batch.operations[result.writeErrors[i].index]
|
|
403
404
|
};
|
|
404
405
|
|
|
405
406
|
bulkResult.writeErrors.push(new WriteError(writeError));
|
|
406
407
|
}
|
|
407
408
|
}
|
|
408
409
|
|
|
409
|
-
if(result.writeConcernError) {
|
|
410
|
+
if (result.writeConcernError) {
|
|
410
411
|
bulkResult.writeConcernErrors.push(new WriteConcernError(result.writeConcernError));
|
|
411
412
|
}
|
|
412
|
-
}
|
|
413
|
+
};
|
|
413
414
|
|
|
414
415
|
//
|
|
415
416
|
// Clone the options
|
|
416
417
|
var cloneOptions = function(options) {
|
|
417
418
|
var clone = {};
|
|
418
419
|
var keys = Object.keys(options);
|
|
419
|
-
for(var i = 0; i < keys.length; i++) {
|
|
420
|
+
for (var i = 0; i < keys.length; i++) {
|
|
420
421
|
clone[keys[i]] = options[keys[i]];
|
|
421
422
|
}
|
|
422
423
|
|
|
423
424
|
return clone;
|
|
424
|
-
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Creates a new BulkWriteError
|
|
429
|
+
*
|
|
430
|
+
* @class
|
|
431
|
+
* @param {Error|string|object} message The error message
|
|
432
|
+
* @param {BulkWriteResult} result The result of the bulk write operation
|
|
433
|
+
* @return {BulkWriteError} A BulkWriteError instance
|
|
434
|
+
* @extends {MongoError}
|
|
435
|
+
*/
|
|
436
|
+
const BulkWriteError = function(error, result) {
|
|
437
|
+
var message = error.err || error.errmsg || error.errMessage || error;
|
|
438
|
+
MongoError.call(this, message);
|
|
439
|
+
|
|
440
|
+
var keys = typeof error === 'object' ? Object.keys(error) : [];
|
|
441
|
+
for (var i = 0; i < keys.length; i++) {
|
|
442
|
+
this[keys[i]] = error[keys[i]];
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
this.name = 'BulkWriteError';
|
|
446
|
+
this.result = result;
|
|
447
|
+
};
|
|
448
|
+
util.inherits(BulkWriteError, MongoError);
|
|
425
449
|
|
|
426
450
|
// Exports symbols
|
|
451
|
+
exports.BulkWriteError = BulkWriteError;
|
|
427
452
|
exports.BulkWriteResult = BulkWriteResult;
|
|
428
453
|
exports.WriteError = WriteError;
|
|
429
454
|
exports.Batch = Batch;
|