@@ -464,50 +464,41 @@ class CoreCursor extends Readable {
464
464
}
465
465
466
466
const result = r . message ;
467
- if ( result . queryFailure ) {
468
- return done ( new MongoError ( result . documents [ 0 ] ) , null ) ;
469
- }
470
467
471
- // Check if we have a command cursor
472
- if (
473
- Array . isArray ( result . documents ) &&
474
- result . documents . length === 1 &&
475
- ( ! cursor . cmd . find || ( cursor . cmd . find && cursor . cmd . virtual === false ) ) &&
476
- ( typeof result . documents [ 0 ] . cursor !== 'string' ||
477
- result . documents [ 0 ] [ '$err' ] ||
478
- result . documents [ 0 ] [ 'errmsg' ] ||
479
- Array . isArray ( result . documents [ 0 ] . result ) )
480
- ) {
481
- // We have an error document, return the error
482
- if ( result . documents [ 0 ] [ '$err' ] || result . documents [ 0 ] [ 'errmsg' ] ) {
483
- return done ( new MongoError ( result . documents [ 0 ] ) , null ) ;
468
+ if ( Array . isArray ( result . documents ) && result . documents . length === 1 ) {
469
+ const document = result . documents [ 0 ] ;
470
+
471
+ if ( result . queryFailure ) {
472
+ return done ( new MongoError ( document ) , null ) ;
484
473
}
485
474
486
- // We have a cursor document
487
- if ( result . documents [ 0 ] . cursor != null && typeof result . documents [ 0 ] . cursor !== 'string' ) {
488
- const id = result . documents [ 0 ] . cursor . id ;
489
- // If we have a namespace change set the new namespace for getmores
490
- if ( result . documents [ 0 ] . cursor . ns ) {
491
- cursor . ns = result . documents [ 0 ] . cursor . ns ;
475
+ // Check if we have a command cursor
476
+ if ( ! cursor . cmd . find || ( cursor . cmd . find && cursor . cmd . virtual === false ) ) {
477
+ // We have an error document, return the error
478
+ if ( document . $err || document . errmsg ) {
479
+ return done ( new MongoError ( document ) , null ) ;
492
480
}
493
- // Promote id to long if needed
494
- cursor . cursorState . cursorId = typeof id === 'number' ? Long . fromNumber ( id ) : id ;
495
- cursor . cursorState . lastCursorId = cursor . cursorState . cursorId ;
496
- cursor . cursorState . operationTime = result . documents [ 0 ] . operationTime ;
497
-
498
- // If we have a firstBatch set it
499
- if ( Array . isArray ( result . documents [ 0 ] . cursor . firstBatch ) ) {
500
- cursor . cursorState . documents = result . documents [ 0 ] . cursor . firstBatch ; //.reverse();
501
- }
502
-
503
- // Return after processing command cursor
504
- return done ( null , result ) ;
505
- }
506
481
507
- if ( Array . isArray ( result . documents [ 0 ] . result ) ) {
508
- cursor . cursorState . documents = result . documents [ 0 ] . result ;
509
- cursor . cursorState . cursorId = Long . ZERO ;
510
- return done ( null , result ) ;
482
+ // We have a cursor document
483
+ if ( document . cursor != null && typeof document . cursor !== 'string' ) {
484
+ const id = document . cursor . id ;
485
+ // If we have a namespace change set the new namespace for getmores
486
+ if ( document . cursor . ns ) {
487
+ cursor . ns = document . cursor . ns ;
488
+ }
489
+ // Promote id to long if needed
490
+ cursor . cursorState . cursorId = typeof id === 'number' ? Long . fromNumber ( id ) : id ;
491
+ cursor . cursorState . lastCursorId = cursor . cursorState . cursorId ;
492
+ cursor . cursorState . operationTime = document . operationTime ;
493
+
494
+ // If we have a firstBatch set it
495
+ if ( Array . isArray ( document . cursor . firstBatch ) ) {
496
+ cursor . cursorState . documents = document . cursor . firstBatch ; //.reverse();
497
+ }
498
+
499
+ // Return after processing command cursor
500
+ return done ( null , result ) ;
501
+ }
511
502
}
512
503
}
513
504
0 commit comments