@@ -277,7 +277,7 @@ Collection.prototype.find = function() {
277
277
278
278
// Ensure the query is an object
279
279
if ( selector != null && typeof selector != 'object' ) {
280
- throw new MongoError ( "query selector must be an object" ) ;
280
+ throw MongoError . create ( { message : "query selector must be an object" , driver : true } ) ;
281
281
}
282
282
283
283
// Build the find command
@@ -357,7 +357,7 @@ Collection.prototype.insertOne = function(doc, options, callback) {
357
357
var self = this ;
358
358
if ( typeof options == 'function' ) callback = options , options = { } ;
359
359
options = options || { } ;
360
- if ( Array . isArray ( doc ) ) return callback ( new MongoError ( 'doc parameter must be an object' ) ) ;
360
+ if ( Array . isArray ( doc ) ) return callback ( MongoError . create ( { message : 'doc parameter must be an object' , driver : true } ) ) ;
361
361
362
362
// Execute using callback
363
363
if ( typeof callback == 'function' ) return insertOne ( self , doc , options , callback ) ;
@@ -384,6 +384,25 @@ var insertOne = function(self, doc, options, callback) {
384
384
} ) ;
385
385
}
386
386
387
+ var mapInserManyResults = function ( docs , r ) {
388
+ var ids = r . getInsertedIds ( ) ;
389
+ var keys = Object . keys ( ids ) ;
390
+ var finalIds = new Array ( keys ) ;
391
+
392
+ for ( var i = 0 ; i < keys . length ; i ++ ) {
393
+ if ( ids [ keys [ i ] ] . _id ) {
394
+ finalIds [ ids [ keys [ i ] ] . index ] = ids [ keys [ i ] ] . _id ;
395
+ }
396
+ }
397
+
398
+ return {
399
+ result : { ok : 1 , n : r . insertedCount } ,
400
+ ops : docs ,
401
+ insertedCount : r . insertedCount ,
402
+ insertedIds : finalIds
403
+ }
404
+ }
405
+
387
406
/**
388
407
* Inserts an array of documents into MongoDB.
389
408
* @method
@@ -400,33 +419,47 @@ var insertOne = function(self, doc, options, callback) {
400
419
Collection . prototype . insertMany = function ( docs , options , callback ) {
401
420
var self = this ;
402
421
if ( typeof options == 'function' ) callback = options , options = { } ;
403
- options = options || { } ;
404
- if ( ! Array . isArray ( docs ) ) return callback ( new MongoError ( 'docs parameter must be an array of documents' ) ) ;
422
+ options = options || { ordered :true } ;
423
+ if ( ! Array . isArray ( docs ) ) return callback ( MongoError . create ( { message : 'docs parameter must be an array of documents' , driver :true } ) ) ;
424
+
425
+ // Get the write concern options
426
+ if ( typeof options . checkKeys != 'boolean' ) {
427
+ options . checkKeys = true ;
428
+ }
429
+
430
+ // If keep going set unordered
431
+ options [ 'serializeFunctions' ] = options [ 'serializeFunctions' ] || self . s . serializeFunctions ;
432
+
433
+ // Do we have keepGoing legacy o
434
+ if ( options . forceServerObjectId == null || options . forceServerObjectId == false ) {
435
+ // Add _id if not specified
436
+ for ( var i = 0 ; i < docs . length ; i ++ ) {
437
+ if ( docs [ i ] . _id == null ) docs [ i ] . _id = self . s . pkFactory . createPk ( ) ;
438
+ }
439
+ }
440
+
441
+ // Generate the bulk write operations
442
+ var operations = [ {
443
+ insertMany : docs
444
+ } ] ;
405
445
406
446
// Execute using callback
407
- if ( typeof callback == 'function' ) return insertMany ( self , docs , options , callback ) ;
447
+ if ( typeof callback == 'function' ) return bulkWrite ( self , operations , options , function ( err , r ) {
448
+ if ( err ) return callback ( err ) ;
449
+ if ( r . hasWriteErrors ( ) ) return callback ( r ) ;
450
+ callback ( null , mapInserManyResults ( docs , r ) ) ;
451
+ } ) ;
408
452
409
453
// Return a Promise
410
454
return new this . s . promiseLibrary ( function ( resolve , reject ) {
411
- insertMany ( self , docs , options , function ( err , r ) {
455
+ bulkWrite ( self , operations , options , function ( err , r ) {
412
456
if ( err ) return reject ( err ) ;
413
- resolve ( r ) ;
414
- } ) ;
415
- } ) ;
416
- }
457
+ if ( r . hasWriteErrors ( ) ) {
458
+ return reject ( r ) ;
459
+ }
417
460
418
- var insertMany = function ( self , docs , options , callback ) {
419
- insertDocuments ( self , docs , options , function ( err , r ) {
420
- if ( callback == null ) return ;
421
- if ( err && callback ) return callback ( err ) ;
422
- if ( r == null ) return callback ( null , { result : { ok :1 } } ) ;
423
- r . insertedCount = r . result . n ;
424
- var ids = [ ] ;
425
- for ( var i = 0 ; i < docs . length ; i ++ ) {
426
- if ( docs [ i ] . _id ) ids . push ( docs [ i ] . _id ) ;
427
- }
428
- r . insertedIds = ids ;
429
- if ( callback ) callback ( null , r ) ;
461
+ resolve ( mapInserManyResults ( docs , r ) ) ;
462
+ } ) ;
430
463
} ) ;
431
464
}
432
465
@@ -482,7 +515,9 @@ Collection.prototype.bulkWrite = function(operations, options, callback) {
482
515
if ( typeof options == 'function' ) callback = options , options = { } ;
483
516
options = options || { ordered :true } ;
484
517
485
- if ( ! Array . isArray ( operations ) ) throw new MongoError ( "operations must be an array of documents" ) ;
518
+ if ( ! Array . isArray ( operations ) ) {
519
+ throw MongoError . create ( { message : "operations must be an array of documents" , driver :true } ) ;
520
+ }
486
521
487
522
// Execute using callback
488
523
if ( typeof callback == 'function' ) return bulkWrite ( self , operations , options , callback ) ;
@@ -497,7 +532,7 @@ Collection.prototype.bulkWrite = function(operations, options, callback) {
497
532
}
498
533
499
534
var bulkWrite = function ( self , operations , options , callback ) {
500
- var bulk = options . ordered == true || options . ordered == null ? self . initializeOrderedBulkOp ( ) : self . initializeUnorderedBulkOp ( ) ;
535
+ var bulk = options . ordered == true || options . ordered == null ? self . initializeOrderedBulkOp ( options ) : self . initializeUnorderedBulkOp ( options ) ;
501
536
502
537
// for each op go through and add to the bulk
503
538
for ( var i = 0 ; i < operations . length ; i ++ ) {
@@ -510,6 +545,7 @@ var bulkWrite = function(self, operations, options, callback) {
510
545
511
546
// Execute the bulk
512
547
bulk . execute ( writeCon , function ( err , r ) {
548
+ if ( err ) return callback ( err ) ;
513
549
r . insertedCount = r . nInserted ;
514
550
r . matchedCount = r . nMatched ;
515
551
r . modifiedCount = r . nModified || 0 ;
@@ -518,6 +554,9 @@ var bulkWrite = function(self, operations, options, callback) {
518
554
r . upsertedIds = { } ;
519
555
r . insertedIds = { } ;
520
556
557
+ // Update the n
558
+ r . n = r . insertedCount ;
559
+
521
560
// Inserted documents
522
561
var inserted = r . getInsertedIds ( ) ;
523
562
// Map inserted ids
@@ -601,18 +640,14 @@ var insertDocuments = function(self, docs, options, callback) {
601
640
Collection . prototype . insert = function ( docs , options , callback ) {
602
641
var self = this ;
603
642
if ( typeof options == 'function' ) callback = options , options = { } ;
604
- options = options || { } ;
643
+ options = options || { ordered :false } ;
644
+ docs = ! Array . isArray ( docs ) ? [ docs ] : docs ;
605
645
606
- // Execute using callback
607
- if ( typeof callback == 'function' ) return insertDocuments ( self , docs , options , callback ) ;
646
+ if ( options . keepGoing == true ) {
647
+ options . ordered = false ;
648
+ }
608
649
609
- // Return a Promise
610
- return new this . s . promiseLibrary ( function ( resolve , reject ) {
611
- insertDocuments ( self , docs , options , function ( err , r ) {
612
- if ( err ) return reject ( err ) ;
613
- resolve ( r ) ;
614
- } ) ;
615
- } ) ;
650
+ return this . insertMany ( docs , options , callback ) ;
616
651
}
617
652
618
653
/**
@@ -1172,7 +1207,10 @@ Collection.prototype.options = function(callback) {
1172
1207
var options = function ( self , callback ) {
1173
1208
self . s . db . listCollections ( { name : self . s . name } ) . toArray ( function ( err , collections ) {
1174
1209
if ( err ) return handleCallback ( callback , err ) ;
1175
- if ( collections . length == 0 ) return handleCallback ( callback , new MongoError ( f ( "collection %s not found" , self . s . namespace ) ) ) ;
1210
+ if ( collections . length == 0 ) {
1211
+ return handleCallback ( callback , MongoError . create ( { message : f ( "collection %s not found" , self . s . namespace ) , driver :true } ) ) ;
1212
+ }
1213
+
1176
1214
handleCallback ( callback , err , collections [ 0 ] . options || null ) ;
1177
1215
} ) ;
1178
1216
}
0 commit comments