@@ -378,14 +378,14 @@ Collection.prototype.find = function(query, options, callback) {
378
378
// Decorate find command with collation options
379
379
decorateWithCollation ( findCommand , this , options ) ;
380
380
381
- // Create the cursor
382
- if ( typeof callback === 'function' )
383
- return handleCallback (
384
- callback ,
385
- null ,
386
- this . s . topology . cursor ( this . s . namespace , findCommand , newOptions )
387
- ) ;
388
- return this . s . topology . cursor ( this . s . namespace , findCommand , newOptions ) ;
381
+ const cursor = this . s . topology . cursor ( this . s . namespace , findCommand , newOptions ) ;
382
+
383
+ // automatically call map on the cursor if the map option is set
384
+ if ( typeof this . s . options . map === 'function' ) {
385
+ cursor . map ( this . s . options . map ) ;
386
+ }
387
+
388
+ return typeof callback === 'function' ? handleCallback ( callback , null , cursor ) : cursor ;
389
389
} ;
390
390
391
391
/**
@@ -491,19 +491,7 @@ Collection.prototype.insertMany = function(docs, options, callback) {
491
491
// If keep going set unordered
492
492
options [ 'serializeFunctions' ] = options [ 'serializeFunctions' ] || self . s . serializeFunctions ;
493
493
494
- // Set up the force server object id
495
- var forceServerObjectId =
496
- typeof options . forceServerObjectId === 'boolean'
497
- ? options . forceServerObjectId
498
- : self . s . db . options . forceServerObjectId ;
499
-
500
- // Do we want to force the server to assign the _id key
501
- if ( forceServerObjectId !== true ) {
502
- // Add _id if not specified
503
- for ( var i = 0 ; i < docs . length ; i ++ ) {
504
- if ( docs [ i ] . _id == null ) docs [ i ] . _id = self . s . pkFactory . createPk ( ) ;
505
- }
506
- }
494
+ docs = prepareDocs ( this , docs , options ) ;
507
495
508
496
// Generate the bulk write operations
509
497
var operations = [
@@ -683,18 +671,7 @@ var insertDocuments = function(self, docs, options, callback) {
683
671
if ( finalOptions . keepGoing === true ) finalOptions . ordered = false ;
684
672
finalOptions [ 'serializeFunctions' ] = options [ 'serializeFunctions' ] || self . s . serializeFunctions ;
685
673
686
- // Set up the force server object id
687
- var forceServerObjectId =
688
- typeof options . forceServerObjectId === 'boolean'
689
- ? options . forceServerObjectId
690
- : self . s . db . options . forceServerObjectId ;
691
-
692
- // Add _id if not specified
693
- if ( forceServerObjectId !== true ) {
694
- for ( var i = 0 ; i < docs . length ; i ++ ) {
695
- if ( docs [ i ] . _id === void 0 ) docs [ i ] . _id = self . s . pkFactory . createPk ( ) ;
696
- }
697
- }
674
+ docs = prepareDocs ( self , docs , options ) ;
698
675
699
676
// File inserts
700
677
self . s . topology . insert ( self . s . namespace , docs , finalOptions , function ( err , result ) {
@@ -909,6 +886,10 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
909
886
options . ignoreUndefined = this . s . options . ignoreUndefined ;
910
887
}
911
888
889
+ if ( typeof this . s . options . unmap === 'function' ) {
890
+ doc = this . s . options . unmap ( doc ) ;
891
+ }
892
+
912
893
return executeOperation ( this . s . topology , replaceOne , [ this , filter , doc , options , callback ] ) ;
913
894
} ;
914
895
@@ -2253,6 +2234,11 @@ var findAndModify = function(self, query, sort, doc, options, callback) {
2253
2234
// Execute the command
2254
2235
self . s . db . command ( queryObject , finalOptions , function ( err , result ) {
2255
2236
if ( err ) return handleCallback ( callback , err , null ) ;
2237
+
2238
+ if ( result && result . value && typeof self . s . options . map === 'function' ) {
2239
+ result . value = self . s . options . map ( result . value ) ;
2240
+ }
2241
+
2256
2242
return handleCallback ( callback , null , result ) ;
2257
2243
} ) ;
2258
2244
} ;
@@ -3028,4 +3014,28 @@ var getReadPreference = function(self, options, db) {
3028
3014
return options ;
3029
3015
} ;
3030
3016
3017
+ // modifies documents before being inserted or updated
3018
+ const prepareDocs = function ( self , docs , options ) {
3019
+ const forceServerObjectId =
3020
+ typeof options . forceServerObjectId === 'boolean'
3021
+ ? options . forceServerObjectId
3022
+ : self . s . db . options . forceServerObjectId ;
3023
+
3024
+ const unmap = typeof self . s . options . unmap === 'function' ? self . s . options . unmap : false ;
3025
+
3026
+ // no need to modify the docs if server sets the ObjectId
3027
+ // and unmap collection option is unset
3028
+ if ( forceServerObjectId === true && ! unmap ) {
3029
+ return docs ;
3030
+ }
3031
+
3032
+ return docs . map ( function ( doc ) {
3033
+ if ( forceServerObjectId !== true && doc . _id == null ) {
3034
+ doc . _id = self . s . pkFactory . createPk ( ) ;
3035
+ }
3036
+
3037
+ return unmap ? unmap ( doc ) : doc ;
3038
+ } ) ;
3039
+ } ;
3040
+
3031
3041
module . exports = Collection ;
0 commit comments