@@ -175,33 +175,14 @@ function checkForAtomicOperators(update) {
175
175
function count ( coll , query , options , callback ) {
176
176
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
177
177
options = Object . assign ( { } , options ) ;
178
+ options . collectionName = coll . s . name ;
178
179
179
- const skip = options . skip ;
180
- const limit = options . limit ;
181
- const hint = options . hint ;
182
- const maxTimeMS = options . maxTimeMS ;
183
- query = query || { } ;
184
-
185
- // Final query
186
- const cmd = {
187
- count : coll . s . name ,
188
- query : query
189
- } ;
190
-
191
- // Add limit, skip and maxTimeMS if defined
192
- if ( typeof skip === 'number' ) cmd . skip = skip ;
193
- if ( typeof limit === 'number' ) cmd . limit = limit ;
194
- if ( typeof maxTimeMS === 'number' ) cmd . maxTimeMS = maxTimeMS ;
195
- if ( hint ) cmd . hint = hint ;
196
-
197
- // Ensure we have the right read preference inheritance
198
- options . readPreference = resolveReadPreference ( options , { db : coll . s . db , collection : coll } ) ;
199
-
200
- // Do we have a readConcern specified
201
- decorateWithReadConcern ( cmd , coll , options ) ;
180
+ options . readPreference = resolveReadPreference ( options , {
181
+ db : coll . s . db ,
182
+ collection : coll
183
+ } ) ;
202
184
203
- // Have we specified collation
204
- decorateWithCollation ( cmd , coll , options ) ;
185
+ const cmd = buildCountCommand ( coll , query , options ) ;
205
186
206
187
executeCommand ( coll . s . db , cmd , options , ( err , result ) => {
207
188
if ( err ) return handleCallback ( callback , err ) ;
@@ -236,6 +217,51 @@ function countDocuments(coll, query, options, callback) {
236
217
} ) ;
237
218
}
238
219
220
+ /**
221
+ * Build the count command.
222
+ *
223
+ * @method
224
+ * @param {collectionOrCursor } an instance of a collection or cursor
225
+ * @param {object } query The query for the count.
226
+ * @param {object } [options] Optional settings. See Collection.prototype.count and Cursor.prototype.count for a list of options.
227
+ */
228
+ function buildCountCommand ( collectionOrCursor , query , options ) {
229
+ const skip = options . skip ;
230
+ const limit = options . limit ;
231
+ let hint = options . hint ;
232
+ const maxTimeMS = options . maxTimeMS ;
233
+ query = query || { } ;
234
+
235
+ // Final query
236
+ const cmd = {
237
+ count : options . collectionName ,
238
+ query : query
239
+ } ;
240
+
241
+ // check if collectionOrCursor is a cursor by using cursor.s.numberOfRetries
242
+ if ( collectionOrCursor . s . numberOfRetries ) {
243
+ if ( collectionOrCursor . s . options . hint ) {
244
+ hint = collectionOrCursor . s . options . hint ;
245
+ } else if ( collectionOrCursor . s . cmd . hint ) {
246
+ hint = collectionOrCursor . s . cmd . hint ;
247
+ }
248
+ decorateWithCollation ( cmd , collectionOrCursor , collectionOrCursor . s . cmd ) ;
249
+ } else {
250
+ decorateWithCollation ( cmd , collectionOrCursor , options ) ;
251
+ }
252
+
253
+ // Add limit, skip and maxTimeMS if defined
254
+ if ( typeof skip === 'number' ) cmd . skip = skip ;
255
+ if ( typeof limit === 'number' ) cmd . limit = limit ;
256
+ if ( typeof maxTimeMS === 'number' ) cmd . maxTimeMS = maxTimeMS ;
257
+ if ( hint ) cmd . hint = hint ;
258
+
259
+ // Do we have a readConcern specified
260
+ decorateWithReadConcern ( cmd , collectionOrCursor ) ;
261
+
262
+ return cmd ;
263
+ }
264
+
239
265
/**
240
266
* Create an index on the db and collection.
241
267
*
@@ -1391,6 +1417,7 @@ module.exports = {
1391
1417
checkForAtomicOperators,
1392
1418
count,
1393
1419
countDocuments,
1420
+ buildCountCommand,
1394
1421
createIndex,
1395
1422
createIndexes,
1396
1423
deleteMany,
0 commit comments