lib/mongodb/mongodb-core/lib/wireprotocol/3_2_support.js
function executeWrite(pool, bson, type, opsField, ns, ops, options, callback) {
  if (ops.length === 0) throw new MongoError('insert must contain at least one document');
  if (typeof options === 'function') {
    callback = options;
    options = {};
    options = options || {};
  }

  // Split the ns up to get db and collection
  const p = ns.split('.');
  const d = p.shift();
  // Options
  const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  const writeConcern = options.writeConcern;

  // return skeleton
  const writeCommand = {};
  writeCommand[type] = p.join('.');
  writeCommand[opsField] = ops;
  writeCommand.ordered = ordered;

  // Did we specify a write concern
  if (writeConcern && Object.keys(writeConcern).length > 0) {
    writeCommand.writeConcern = writeConcern;
  }

  // If we have collation passed in
  if (options.collation) {
    for (let i = 0; i < writeCommand[opsField].length; i++) {
      if (!writeCommand[opsField][i].collation) {
        writeCommand[opsField][i].collation = options.collation;
      }
    }
  }

  // Do we have bypassDocumentValidation set, then enable it on the write command
  if (options.bypassDocumentValidation === true) {
    writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
  }

  // optionally decorate command with transactions data
  const err = decorateWithSessionsData(writeCommand, options.session, options, callback);
  if (err) {
    return callback(err, null);
  }

  // Options object
  const opts = { command: true };
  if (typeof options.session !== 'undefined') opts.session = options.session;
  const queryOptions = { checkKeys: false, numberToSkip: 0, numberToReturn: 1 };
  if (type === 'insert') queryOptions.checkKeys = true;
  if (typeof options.checkKeys === 'boolean') queryOptions.checkKeys = options.checkKeys;

  // Ensure we support serialization of functions
  if (options.serializeFunctions) queryOptions.serializeFunctions = options.serializeFunctions;
  // Do not serialize the undefined fields
  if (options.ignoreUndefined) queryOptions.ignoreUndefined = options.ignoreUndefined;

  try {
    // Create write command
    const cmd = new Query(bson, `${d}.$cmd`, writeCommand, queryOptions);
    // Execute command
    pool.write(cmd, opts, callback);
  } catch (err) {
    callback(err);
  }
}
function executeWrite(pool, bson, type, opsField, ns, ops, options, callback) {
  if (ops.length === 0) throw new MongoError('insert must contain at least one document');
  if (typeof options === 'function') {
    callback = options;
    options = {};
    options = options || {};
  }

  // Split the ns up to get db and collection
  const p = ns.split('.');
  const d = p.shift();
  // Options
  const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  const writeConcern = options.writeConcern;

  // return skeleton
  const writeCommand = {};
  writeCommand[type] = p.join('.');
  writeCommand[opsField] = ops;
  writeCommand.ordered = ordered;

  // Did we specify a write concern
  if (writeConcern && Object.keys(writeConcern).length > 0) {
    writeCommand.writeConcern = writeConcern;
  }

  // If we have collation passed in
  if (options.collation) {
    for (let i = 0; i < writeCommand[opsField].length; i++) {
      if (!writeCommand[opsField][i].collation) {
        writeCommand[opsField][i].collation = options.collation;
      }
    }
  }

  // Do we have bypassDocumentValidation set, then enable it on the write command
  if (options.bypassDocumentValidation === true) {
    writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
  }

  // optionally decorate command with transactions data
  const err = decorateWithSessionsData(writeCommand, options.session, options, callback);
  if (err) {
    return callback(err, null);
  }

  // Options object
  const opts = { command: true };
  if (typeof options.session !== 'undefined') opts.session = options.session;
  const queryOptions = { checkKeys: false, numberToSkip: 0, numberToReturn: 1 };
  if (type === 'insert') queryOptions.checkKeys = true;
  if (typeof options.checkKeys === 'boolean') queryOptions.checkKeys = options.checkKeys;

  // Ensure we support serialization of functions
  if (options.serializeFunctions) queryOptions.serializeFunctions = options.serializeFunctions;
  // Do not serialize the undefined fields
  if (options.ignoreUndefined) queryOptions.ignoreUndefined = options.ignoreUndefined;

  try {
    // Create write command
    const cmd = new Query(bson, `${d}.$cmd`, writeCommand, queryOptions);
    // Execute command
    pool.write(cmd, opts, callback);
  } catch (err) {
    callback(err);
  }
}
function executeWrite ( pool , bson , type , opsField , ns , ops , options , callback ) { if ( ops . length === 0 ) throw new MongoError ( 'insert must contain at least one document' ) ; if ( typeof options === 'function' ) { callback = options ; options = { } ; options = options || { } ; } // Split the ns up to get db and collection const p = ns . split ( '.' ) ; const d = p . shift ( ) ; // Options const ordered = typeof options . ordered === 'boolean' ? options . ordered : true ; const writeConcern = options . writeConcern ; // return skeleton const writeCommand = { } ; writeCommand [ type ] = p . join ( '.' ) ; writeCommand [ opsField ] = ops ; writeCommand . ordered = ordered ; // Did we specify a write concern if ( writeConcern && Object . keys ( writeConcern ) . length > 0 ) { writeCommand . writeConcern = writeConcern ; } // If we have collation passed in if ( options . collation ) { for ( let i = 0 ; i < writeCommand [ opsField ] . length ; i ++ ) { if ( ! writeCommand [ opsField ] [ i ] . collation ) { writeCommand [ opsField ] [ i ] . collation = options . collation ; } } } // Do we have bypassDocumentValidation set, then enable it on the write command if ( options . bypassDocumentValidation === true ) { writeCommand . bypassDocumentValidation = options . bypassDocumentValidation ; } // optionally decorate command with transactions data const err = decorateWithSessionsData ( writeCommand , options . session , options , callback ) ; if ( err ) { return callback ( err , null ) ; } // Options object const opts = { command : true } ; if ( typeof options . session !== 'undefined' ) opts . session = options . session ; const queryOptions = { checkKeys : false , numberToSkip : 0 , numberToReturn : 1 } ; if ( type === 'insert' ) queryOptions . checkKeys = true ; if ( typeof options . checkKeys === 'boolean' ) queryOptions . checkKeys = options . checkKeys ; // Ensure we support serialization of functions if ( options . serializeFunctions ) queryOptions . serializeFunctions = options . serializeFunctions ; // Do not serialize the undefined fields if ( options . ignoreUndefined ) queryOptions . ignoreUndefined = options . ignoreUndefined ; try { // Create write command const cmd = new Query ( bson , ` ${ d } ` , writeCommand , queryOptions ) ; // Execute command pool . write ( cmd , opts , callback ) ; } catch ( err ) { callback ( err ) ; } }
Execute a write operation
Execute a write operation