@@ -1748,7 +1748,33 @@ Query.prototype._count = function(callback) {
17481748} ;
17491749
17501750/**
1751- * Specifying this query as a `count` query.
1751+ * Thunk around countDocuments()
1752+ *
1753+ * @param {Function } [callback]
1754+ * @see countDocuments http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
1755+ * @api private
1756+ */
1757+
1758+ Query . prototype . _countDocuments = function ( callback ) {
1759+ try {
1760+ this . cast ( this . model ) ;
1761+ } catch ( err ) {
1762+ this . error ( err ) ;
1763+ }
1764+
1765+ if ( this . error ( ) ) {
1766+ return callback ( this . error ( ) ) ;
1767+ }
1768+
1769+ const conds = this . _conditions ;
1770+ const options = this . _optionsForExec ( ) ;
1771+
1772+ this . _collection . collection . countDocuments ( conds , options , utils . tick ( callback ) ) ;
1773+ } ;
1774+
1775+ /**
1776+ * Specifies this query as a `count` query. This method is deprecated, use
1777+ * `countDocuments()` instead.
17521778 *
17531779 * Passing a `callback` executes the query.
17541780 *
@@ -1769,6 +1795,7 @@ Query.prototype._count = function(callback) {
17691795 * console.log('there are %d kittens', count);
17701796 * })
17711797 *
1798+ * @deprecated
17721799 * @param {Object } [criteria] mongodb selector
17731800 * @param {Function } [callback] optional params are (error, count)
17741801 * @return {Query } this
@@ -1798,6 +1825,58 @@ Query.prototype.count = function(conditions, callback) {
17981825 return this ;
17991826} ;
18001827
1828+ /**
1829+ * Specifies this query as a `countDocuments()` query. Behaves like `count()`,
1830+ * except for [`$where` and a couple geospatial operators](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments).
1831+ *
1832+ * Passing a `callback` executes the query.
1833+ *
1834+ * This function triggers the following middleware.
1835+ *
1836+ * - `countDocuments()`
1837+ *
1838+ * ####Example:
1839+ *
1840+ * const countQuery = model.where({ 'color': 'black' }).countDocuments();
1841+ *
1842+ * query.countDocuments({ color: 'black' }).count(callback);
1843+ *
1844+ * query.countDocuments({ color: 'black' }, callback);
1845+ *
1846+ * query.where('color', 'black').countDocuments(function(err, count) {
1847+ * if (err) return handleError(err);
1848+ * console.log('there are %d kittens', count);
1849+ * });
1850+ *
1851+ * @param {Object } [criteria] mongodb selector
1852+ * @param {Function } [callback] optional params are (error, count)
1853+ * @return {Query } this
1854+ * @see countDocuments http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
1855+ * @api public
1856+ */
1857+
1858+ Query . prototype . countDocuments = function ( conditions , callback ) {
1859+ if ( typeof conditions === 'function' ) {
1860+ callback = conditions ;
1861+ conditions = undefined ;
1862+ }
1863+
1864+ conditions = utils . toObject ( conditions ) ;
1865+
1866+ if ( mquery . canMerge ( conditions ) ) {
1867+ this . merge ( conditions ) ;
1868+ }
1869+
1870+ this . op = 'countDocuments' ;
1871+ if ( ! callback ) {
1872+ return this ;
1873+ }
1874+
1875+ this . _countDocuments ( callback ) ;
1876+
1877+ return this ;
1878+ } ;
1879+
18011880/**
18021881 * Declares or executes a distict() operation.
18031882 *
0 commit comments