MCPcopy
hub / github.com/mongodb/node-mongodb-native / sendCommand

Method sendCommand

src/cmap/connection.ts:506–603  ·  view source on GitHub ↗
(
    ns: MongoDBNamespace,
    command: Document,
    options: CommandOptions & Abortable,
    responseType?: MongoDBResponseConstructor
  )

Source from the content-addressed store, hash-verified

504 }
505
506 private async *sendCommand(
507 ns: MongoDBNamespace,
508 command: Document,
509 options: CommandOptions & Abortable,
510 responseType?: MongoDBResponseConstructor
511 ) {
512 options?.signal?.throwIfAborted();
513
514 const message = this.prepareCommand(ns.db, command, options);
515 let started = 0;
516 if (this.shouldEmitAndLogCommand) {
517 started = processTimeMS();
518 this.emitAndLogCommand(
519 this.monitorCommands,
520 Connection.COMMAND_STARTED,
521 message.databaseName,
522 this.established,
523 new CommandStartedEvent(this, message, this.description.serverConnectionId)
524 );
525 }
526
527 // If `documentsReturnedIn` not set or raw is not enabled, use input bson options
528 // Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
529 const bsonOptions: DeserializeOptions =
530 options.documentsReturnedIn == null || !options.raw
531 ? options
532 : {
533 ...options,
534 raw: false,
535 fieldsAsRaw: { [options.documentsReturnedIn]: true }
536 };
537
538 /** MongoDBResponse instance or subclass */
539 let document: MongoDBResponse | undefined = undefined;
540 /** Cached result of a toObject call */
541 let object: Document | undefined = undefined;
542 try {
543 this.throwIfAborted();
544 for await (document of this.sendWire(message, options, responseType)) {
545 object = undefined;
546 if (options.session != null) {
547 updateSessionFromResponse(options.session, document);
548 }
549
550 if (document.$clusterTime) {
551 this.clusterTime = document.$clusterTime;
552 this.emit(Connection.CLUSTER_TIME_RECEIVED, document.$clusterTime);
553 }
554
555 if (document.ok === 0) {
556 if (options.timeoutContext?.csotEnabled() && document.isMaxTimeExpiredError) {
557 throw new MongoOperationTimeoutError('Server reported a timeout error', {
558 cause: new MongoServerError((object ??= document.toObject(bsonOptions)))
559 });
560 }
561 throw new MongoServerError((object ??= document.toObject(bsonOptions)));
562 }
563

Callers 3

commandMethod · 0.95
exhaustLoopMethod · 0.95
livereload.jsFile · 0.80

Calls 9

throwIfAbortedMethod · 0.95
prepareCommandMethod · 0.95
sendWireMethod · 0.95
processTimeMSFunction · 0.90
emitAndLogCommandMethod · 0.80
emitMethod · 0.65
csotEnabledMethod · 0.45
toObjectMethod · 0.45

Tested by

no test coverage detected