File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const eachAsync = require('../helpers/cursor/eachAsync');
10
10
const helpers = require ( '../queryHelpers' ) ;
11
11
const kareem = require ( 'kareem' ) ;
12
12
const immediate = require ( '../helpers/immediate' ) ;
13
+ const { once } = require ( 'node:events' ) ;
13
14
const util = require ( 'util' ) ;
14
15
15
16
/**
@@ -135,6 +136,25 @@ QueryCursor.prototype._read = function() {
135
136
} ) ;
136
137
} ;
137
138
139
+ /**
140
+ * Returns the underlying cursor from the MongoDB Node driver that this cursor uses.
141
+ *
142
+ * @method getDriverCursor
143
+ * @memberOf QueryCursor
144
+ * @returns {Cursor } MongoDB Node driver cursor instance
145
+ * @instance
146
+ * @api public
147
+ */
148
+
149
+ QueryCursor . prototype . getDriverCursor = async function getDriverCursor ( ) {
150
+ if ( this . cursor ) {
151
+ return this . cursor ;
152
+ }
153
+
154
+ await once ( this , 'cursor' ) ;
155
+ return this . cursor ;
156
+ } ;
157
+
138
158
/**
139
159
* Registers a transform function which subsequently maps documents retrieved
140
160
* via the streams interface or `.next()`
Original file line number Diff line number Diff line change @@ -900,6 +900,25 @@ describe('QueryCursor', function() {
900
900
assert . ok ( err ) ;
901
901
assert . ok ( err . message . includes ( 'skipMiddlewareFunction' ) , err . message ) ;
902
902
} ) ;
903
+
904
+ it ( 'returns the underlying Node driver cursor with getDriverCursor()' , async function ( ) {
905
+ const schema = new mongoose . Schema ( { name : String } ) ;
906
+
907
+ const Movie = db . model ( 'Movie' , schema ) ;
908
+
909
+ await Movie . deleteMany ( { } ) ;
910
+ await Movie . create ( [
911
+ { name : 'Kickboxer' } ,
912
+ { name : 'Ip Man' } ,
913
+ { name : 'Enter the Dragon' }
914
+ ] ) ;
915
+
916
+ const cursor = await Movie . find ( { } ) . cursor ( ) ;
917
+ assert . ok ( ! cursor . cursor ) ;
918
+ const driverCursor = await cursor . getDriverCursor ( ) ;
919
+ assert . ok ( cursor . cursor ) ;
920
+ assert . equal ( driverCursor , cursor . cursor ) ;
921
+ } ) ;
903
922
} ) ;
904
923
905
924
async function delay ( ms ) {
You can’t perform that action at this time.
0 commit comments