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