mongodb 2.1.0-alpha → 2.1.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.
Files changed (63) hide show
  1. package/HISTORY.md +574 -429
  2. package/Makefile +2 -5
  3. package/README.md +108 -15
  4. package/conf.json +17 -13
  5. package/index.js +13 -2
  6. package/lib/admin.js +113 -47
  7. package/lib/aggregation_cursor.js +56 -28
  8. package/lib/apm.js +608 -0
  9. package/lib/bulk/common.js +7 -7
  10. package/lib/bulk/ordered.js +56 -17
  11. package/lib/bulk/unordered.js +52 -14
  12. package/lib/collection.js +671 -212
  13. package/lib/command_cursor.js +60 -32
  14. package/lib/cursor.js +313 -115
  15. package/lib/db.js +264 -105
  16. package/lib/gridfs/chunk.js +26 -29
  17. package/lib/gridfs/grid_store.js +150 -64
  18. package/lib/gridfs-stream/download.js +310 -0
  19. package/lib/gridfs-stream/index.js +335 -0
  20. package/lib/gridfs-stream/upload.js +450 -0
  21. package/lib/metadata.js +64 -0
  22. package/lib/mongo_client.js +69 -39
  23. package/lib/mongos.js +65 -20
  24. package/lib/replset.js +69 -34
  25. package/lib/server.js +35 -1
  26. package/lib/topology_base.js +22 -10
  27. package/lib/url_parser.js +111 -13
  28. package/lib/utils.js +9 -8
  29. package/mongolabs.js +427 -0
  30. package/package.json +8 -6
  31. package/t.js +68 -51
  32. package/test.js +12 -0
  33. package/test_boot/boot.sh +3 -0
  34. package/test_boot/ca.pem +49 -0
  35. package/test_boot/client.pem +48 -0
  36. package/test_boot/client_password.pem +51 -0
  37. package/test_boot/connect.js +29 -0
  38. package/test_boot/data/WiredTiger +2 -0
  39. package/test_boot/data/WiredTiger.lock +1 -0
  40. package/test_boot/data/WiredTiger.turtle +6 -0
  41. package/test_boot/data/WiredTiger.wt +0 -0
  42. package/test_boot/data/WiredTigerLAS.wt +0 -0
  43. package/test_boot/data/_mdb_catalog.wt +0 -0
  44. package/test_boot/data/collection-0-757073248613337118.wt +0 -0
  45. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-44-37Z-00000 +0 -0
  46. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-45-15Z-00000 +0 -0
  47. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-46-31Z-00000 +0 -0
  48. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-47-25Z-00000 +0 -0
  49. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-49-07Z-00000 +0 -0
  50. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-50-41Z-00000 +0 -0
  51. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-50-53Z-00000 +0 -0
  52. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-52-31Z-00000 +0 -0
  53. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-54-53Z-00000 +0 -0
  54. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-55-09Z-00000 +0 -0
  55. package/test_boot/data/diagnostic.data/metrics.2015-10-07T14-55-38Z-00000 +0 -0
  56. package/test_boot/data/index-1-757073248613337118.wt +0 -0
  57. package/test_boot/data/mongod.lock +0 -0
  58. package/test_boot/data/sizeStorer.wt +0 -0
  59. package/test_boot/data/storage.bson +0 -0
  60. package/test_boot/server_password.pem +51 -0
  61. package/.travis.yml +0 -10
  62. package/t1.js +0 -59
  63. package/wercker.yml +0 -19
@@ -9,6 +9,7 @@ var common = require('./common')
9
9
  , BulkWriteResult = common.BulkWriteResult
10
10
  , LegacyOp = common.LegacyOp
11
11
  , ObjectID = require('mongodb-core').BSON.ObjectID
12
+ , Define = require('../metadata')
12
13
  , Batch = common.Batch
13
14
  , mergeBatchResults = common.mergeBatchResults;
14
15
 
@@ -211,11 +212,10 @@ function OrderedBulkOperation(topology, collection, options) {
211
212
  var namespace = collection.collectionName;
212
213
 
213
214
  // Set max byte size
214
- var maxBatchSizeBytes = topology.isMasterDoc.maxBsonObjectSize;
215
- var maxWriteBatchSize = topology.isMasterDoc.maxWriteBatchSize || 1000;
216
-
217
- // Get the capabilities
218
- var capabilities = topology.capabilities();
215
+ var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
216
+ ? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
217
+ var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
218
+ ? topology.isMasterDoc.maxWriteBatchSize : 1000;
219
219
 
220
220
  // Get the write concern
221
221
  var writeConcern = common.writeConcern(shallowClone(options), collection, options);
@@ -262,8 +262,6 @@ function OrderedBulkOperation(topology, collection, options) {
262
262
  , batches: []
263
263
  // Write concern
264
264
  , writeConcern: writeConcern
265
- // Capabilities
266
- , capabilities: capabilities
267
265
  // Max batch size options
268
266
  , maxBatchSizeBytes: maxBatchSizeBytes
269
267
  , maxWriteBatchSize: maxWriteBatchSize
@@ -283,12 +281,22 @@ function OrderedBulkOperation(topology, collection, options) {
283
281
  , collection: collection
284
282
  // Promise Library
285
283
  , promiseLibrary: promiseLibrary
284
+ // Fundamental error
285
+ , err: null
286
+ // Bypass validation
287
+ , bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
286
288
  }
287
289
  }
288
290
 
291
+ var define = OrderedBulkOperation.define = new Define('OrderedBulkOperation', OrderedBulkOperation, false);
292
+
289
293
  OrderedBulkOperation.prototype.raw = function(op) {
290
294
  var key = Object.keys(op)[0];
291
295
 
296
+ // Set up the force server object id
297
+ var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
298
+ ? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
299
+
292
300
  // Update operations
293
301
  if((op.updateOne && op.updateOne.q)
294
302
  || (op.updateMany && op.updateMany.q)
@@ -301,7 +309,7 @@ OrderedBulkOperation.prototype.raw = function(op) {
301
309
  if(op.updateOne || op.updateMany || op.replaceOne) {
302
310
  var multi = op.updateOne || op.replaceOne ? false : true;
303
311
  var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
304
- if(op[key].upsert) operation.upsert = true;
312
+ operation.upsert = op[key].upsert ? true: false;
305
313
  return addToOperationsList(this, common.UPDATE, operation);
306
314
  }
307
315
 
@@ -320,16 +328,16 @@ OrderedBulkOperation.prototype.raw = function(op) {
320
328
 
321
329
  // Insert operations
322
330
  if(op.insertOne && op.insertOne.document == null) {
323
- if(op.insertOne._id == null) op.insertOne._id = new ObjectID();
331
+ if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
324
332
  return addToOperationsList(this, common.INSERT, op.insertOne);
325
333
  } else if(op.insertOne && op.insertOne.document) {
326
- if(op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
334
+ if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
327
335
  return addToOperationsList(this, common.INSERT, op.insertOne.document);
328
336
  }
329
337
 
330
338
  if(op.insertMany) {
331
339
  for(var i = 0; i < op.insertMany.length; i++) {
332
- if(op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
340
+ if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
333
341
  addToOperationsList(this, common.INSERT, op.insertMany[i]);
334
342
  }
335
343
 
@@ -348,7 +356,7 @@ OrderedBulkOperation.prototype.raw = function(op) {
348
356
  * @return {OrderedBulkOperation}
349
357
  */
350
358
  OrderedBulkOperation.prototype.insert = function(document) {
351
- if(document._id == null) document._id = new ObjectID();
359
+ if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
352
360
  return addToOperationsList(this, common.INSERT, document);
353
361
  }
354
362
 
@@ -391,6 +399,11 @@ var executeCommands = function(self, callback) {
391
399
  var batch = self.s.batches.shift();
392
400
 
393
401
  var resultHandler = function(err, result) {
402
+ // Error is a driver related error not a bulk op error, terminate
403
+ if(err && err.driver || err && err.message) {
404
+ return callback(err);
405
+ }
406
+
394
407
  // If we have and error
395
408
  if(err) err.ok = 0;
396
409
  // Merge the results together
@@ -402,7 +415,7 @@ var executeCommands = function(self, callback) {
402
415
  // If we are ordered and have errors and they are
403
416
  // not all replication errors terminate the operation
404
417
  if(self.s.bulkResult.writeErrors.length > 0) {
405
- return callback(self.s.bulkResult.writeErrors[0], new BulkWriteResult(self.s.bulkResult));
418
+ return callback(toError(self.s.bulkResult.writeErrors[0]), new BulkWriteResult(self.s.bulkResult));
406
419
  }
407
420
 
408
421
  // Execute the next command in line
@@ -414,6 +427,26 @@ var executeCommands = function(self, callback) {
414
427
  finalOptions.writeConcern = self.s.writeConcern;
415
428
  }
416
429
 
430
+ // Set an operationIf if provided
431
+ if(self.operationId) {
432
+ resultHandler.operationId = self.operationId;
433
+ }
434
+
435
+ // Serialize functions
436
+ if(self.s.options.serializeFunctions) {
437
+ finalOptions.serializeFunctions = true
438
+ }
439
+
440
+ // Serialize functions
441
+ if(self.s.options.ignoreUndefined) {
442
+ finalOptions.ignoreUndefined = true
443
+ }
444
+
445
+ // Is the bypassDocumentValidation options specific
446
+ if(self.s.bypassDocumentValidation == true) {
447
+ finalOptions.bypassDocumentValidation = true;
448
+ }
449
+
417
450
  try {
418
451
  if(batch.batchType == common.INSERT) {
419
452
  self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
@@ -425,7 +458,7 @@ var executeCommands = function(self, callback) {
425
458
  } catch(err) {
426
459
  // Force top level error
427
460
  err.ok = 0;
428
- // Merge top level error and return
461
+ // Merge top level error and return
429
462
  callback(null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
430
463
  }
431
464
  }
@@ -446,7 +479,7 @@ var executeCommands = function(self, callback) {
446
479
  * @param {number} [options.wtimeout=null] The write concern timeout.
447
480
  * @param {boolean} [options.j=false] Specify a journal write concern.
448
481
  * @param {boolean} [options.fsync=false] Specify a file sync write concern.
449
- * @param {OrderedBulkOperation~resultCallback} callback The result callback
482
+ * @param {OrderedBulkOperation~resultCallback} [callback] The result callback
450
483
  * @throws {MongoError}
451
484
  * @return {Promise} returns Promise if no callback passed
452
485
  */
@@ -468,17 +501,21 @@ OrderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
468
501
  }
469
502
 
470
503
  // Execute using callback
471
- if(typeof callback == 'function') return executeCommands(this, callback);
504
+ if(typeof callback == 'function') {
505
+ return executeCommands(this, callback);
506
+ }
472
507
 
473
508
  // Return a Promise
474
509
  return new this.s.promiseLibrary(function(resolve, reject) {
475
510
  executeCommands(self, function(err, r) {
476
511
  if(err) return reject(err);
477
- resolve(r);
512
+ resolve(r);
478
513
  });
479
514
  });
480
515
  }
481
516
 
517
+ define.classMethod('execute', {callback: true, promise:false});
518
+
482
519
  /**
483
520
  * Returns an unordered batch object
484
521
  * @ignore
@@ -487,4 +524,6 @@ var initializeOrderedBulkOp = function(topology, collection, options) {
487
524
  return new OrderedBulkOperation(topology, collection, options);
488
525
  }
489
526
 
527
+ initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
490
528
  module.exports = initializeOrderedBulkOp;
529
+ module.exports.Bulk = OrderedBulkOperation;
@@ -9,6 +9,7 @@ var common = require('./common')
9
9
  , BulkWriteResult = common.BulkWriteResult
10
10
  , LegacyOp = common.LegacyOp
11
11
  , ObjectID = require('mongodb-core').BSON.ObjectID
12
+ , Define = require('../metadata')
12
13
  , Batch = common.Batch
13
14
  , mergeBatchResults = common.mergeBatchResults;
14
15
 
@@ -227,12 +228,11 @@ var UnorderedBulkOperation = function(topology, collection, options) {
227
228
  // Handle to the bson serializer, used to calculate running sizes
228
229
  var bson = topology.bson;
229
230
 
230
- // Get the capabilities
231
- var capabilities = topology.capabilities();
232
-
233
231
  // Set max byte size
234
- var maxBatchSizeBytes = topology.isMasterDoc.maxBsonObjectSize;
235
- var maxWriteBatchSize = topology.isMasterDoc.maxWriteBatchSize || 1000;
232
+ var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
233
+ ? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
234
+ var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
235
+ ? topology.isMasterDoc.maxWriteBatchSize : 1000;
236
236
 
237
237
  // Get the write concern
238
238
  var writeConcern = common.writeConcern(shallowClone(options), collection, options);
@@ -273,8 +273,6 @@ var UnorderedBulkOperation = function(topology, collection, options) {
273
273
  , batches: []
274
274
  // Write concern
275
275
  , writeConcern: writeConcern
276
- // Capabilities
277
- , capabilities: capabilities
278
276
  // Max batch size options
279
277
  , maxBatchSizeBytes: maxBatchSizeBytes
280
278
  , maxWriteBatchSize: maxWriteBatchSize
@@ -294,9 +292,13 @@ var UnorderedBulkOperation = function(topology, collection, options) {
294
292
  , collection: collection
295
293
  // Promise Library
296
294
  , promiseLibrary: promiseLibrary
295
+ // Bypass validation
296
+ , bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
297
297
  }
298
298
  }
299
299
 
300
+ var define = UnorderedBulkOperation.define = new Define('UnorderedBulkOperation', UnorderedBulkOperation, false);
301
+
300
302
  /**
301
303
  * Add a single insert document to the bulk operation
302
304
  *
@@ -305,7 +307,7 @@ var UnorderedBulkOperation = function(topology, collection, options) {
305
307
  * @return {UnorderedBulkOperation}
306
308
  */
307
309
  UnorderedBulkOperation.prototype.insert = function(document) {
308
- if(document._id == null) document._id = new ObjectID();
310
+ if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
309
311
  return addToOperationsList(this, common.INSERT, document);
310
312
  }
311
313
 
@@ -340,6 +342,10 @@ Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
340
342
  UnorderedBulkOperation.prototype.raw = function(op) {
341
343
  var key = Object.keys(op)[0];
342
344
 
345
+ // Set up the force server object id
346
+ var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
347
+ ? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
348
+
343
349
  // Update operations
344
350
  if((op.updateOne && op.updateOne.q)
345
351
  || (op.updateMany && op.updateMany.q)
@@ -371,15 +377,16 @@ UnorderedBulkOperation.prototype.raw = function(op) {
371
377
 
372
378
  // Insert operations
373
379
  if(op.insertOne && op.insertOne.document == null) {
374
- if(op.insertOne._id == null) op.insertOne._id = new ObjectID();
380
+ if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
375
381
  return addToOperationsList(this, common.INSERT, op.insertOne);
376
382
  } else if(op.insertOne && op.insertOne.document) {
377
- if(op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
383
+ if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
378
384
  return addToOperationsList(this, common.INSERT, op.insertOne.document);
379
385
  }
380
386
 
381
387
  if(op.insertMany) {
382
388
  for(var i = 0; i < op.insertMany.length; i++) {
389
+ if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
383
390
  addToOperationsList(this, common.INSERT, op.insertMany[i]);
384
391
  }
385
392
 
@@ -399,11 +406,31 @@ var executeBatch = function(self, batch, callback) {
399
406
  }
400
407
 
401
408
  var resultHandler = function(err, result) {
409
+ // Error is a driver related error not a bulk op error, terminate
410
+ if(err && err.driver || err && err.message) {
411
+ return callback(err);
412
+ }
413
+
402
414
  // If we have and error
403
415
  if(err) err.ok = 0;
404
416
  callback(null, mergeBatchResults(false, batch, self.s.bulkResult, err, result));
405
417
  }
406
418
 
419
+ // Set an operationIf if provided
420
+ if(self.operationId) {
421
+ resultHandler.operationId = self.operationId;
422
+ }
423
+
424
+ // Serialize functions
425
+ if(self.s.options.serializeFunctions) {
426
+ finalOptions.serializeFunctions = true
427
+ }
428
+
429
+ // Is the bypassDocumentValidation options specific
430
+ if(self.s.bypassDocumentValidation == true) {
431
+ finalOptions.bypassDocumentValidation = true;
432
+ }
433
+
407
434
  try {
408
435
  if(batch.batchType == common.INSERT) {
409
436
  self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
@@ -415,7 +442,7 @@ var executeBatch = function(self, batch, callback) {
415
442
  } catch(err) {
416
443
  // Force top level error
417
444
  err.ok = 0;
418
- // Merge top level error and return
445
+ // Merge top level error and return
419
446
  callback(null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
420
447
  }
421
448
  }
@@ -424,14 +451,21 @@ var executeBatch = function(self, batch, callback) {
424
451
  // Execute all the commands
425
452
  var executeBatches = function(self, callback) {
426
453
  var numberOfCommandsToExecute = self.s.batches.length;
454
+ var error = null;
427
455
  // Execute over all the batches
428
456
  for(var i = 0; i < self.s.batches.length; i++) {
429
457
  executeBatch(self, self.s.batches[i], function(err, result) {
458
+ // Driver layer error capture it
459
+ if(err) error = err;
460
+ // Count down the number of commands left to execute
430
461
  numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
431
462
 
432
463
  // Execute
433
464
  if(numberOfCommandsToExecute == 0) {
434
- var error = self.s.bulkResult.writeErrors.length > 0 ? self.s.bulkResult.writeErrors[0] : null;
465
+ // Driver level error
466
+ if(error) return callback(error);
467
+ // Treat write errors
468
+ var error = self.s.bulkResult.writeErrors.length > 0 ? toError(self.s.bulkResult.writeErrors[0]) : null;
435
469
  callback(error, new BulkWriteResult(self.s.bulkResult));
436
470
  }
437
471
  });
@@ -454,7 +488,7 @@ var executeBatches = function(self, callback) {
454
488
  * @param {number} [options.wtimeout=null] The write concern timeout.
455
489
  * @param {boolean} [options.j=false] Specify a journal write concern.
456
490
  * @param {boolean} [options.fsync=false] Specify a file sync write concern.
457
- * @param {UnorderedBulkOperation~resultCallback} callback The result callback
491
+ * @param {UnorderedBulkOperation~resultCallback} [callback] The result callback
458
492
  * @throws {MongoError}
459
493
  * @return {Promise} returns Promise if no callback passed
460
494
  */
@@ -484,11 +518,13 @@ UnorderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
484
518
  return new this.s.promiseLibrary(function(resolve, reject) {
485
519
  executeBatches(self, function(err, r) {
486
520
  if(err) return reject(err);
487
- resolve(r);
521
+ resolve(r);
488
522
  });
489
523
  });
490
524
  }
491
525
 
526
+ define.classMethod('execute', {callback: true, promise:false});
527
+
492
528
  /**
493
529
  * Returns an unordered batch object
494
530
  * @ignore
@@ -497,4 +533,6 @@ var initializeUnorderedBulkOp = function(topology, collection, options) {
497
533
  return new UnorderedBulkOperation(topology, collection, options);
498
534
  }
499
535
 
536
+ initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
500
537
  module.exports = initializeUnorderedBulkOp;
538
+ module.exports.Bulk = UnorderedBulkOperation;