Skip to content

Commit a5b001e

Browse files
committed
Merged pull request #542
2 parents 71d76b1 + 5464f18 commit a5b001e

15 files changed

+852
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
source:
2+
file: apiargs-MongoDBCollection-common-option.yaml
3+
ref: collation
4+
---
5+
arg_name: option
6+
name: hint
7+
type: string|array|object
8+
description: |
9+
The index to use. Specify either the index name as a string or the index key
10+
pattern as a document. If specified, then the query system will only consider
11+
plans using the hinted index.
12+
interface: phpmethod
13+
operation: ~
14+
optional: true
15+
---
16+
arg_name: option
17+
name: limit
18+
type: integer
19+
description: |
20+
The maximum number of matching documents to return.
21+
interface: phpmethod
22+
operation: ~
23+
optional: true
24+
---
25+
source:
26+
file: apiargs-common-option.yaml
27+
ref: maxTimeMS
28+
---
29+
source:
30+
file: apiargs-MongoDBCollection-common-option.yaml
31+
ref: readConcern
32+
---
33+
source:
34+
file: apiargs-MongoDBCollection-common-option.yaml
35+
ref: readPreference
36+
---
37+
source:
38+
file: apiargs-common-option.yaml
39+
ref: session
40+
---
41+
arg_name: option
42+
name: skip
43+
type: integer
44+
description: |
45+
The number of matching documents to skip before returning results.
46+
interface: phpmethod
47+
operation: ~
48+
optional: true
49+
...
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source:
2+
file: apiargs-MongoDBCollection-common-param.yaml
3+
ref: $filter
4+
optional: true
5+
replacement:
6+
action: " to count"
7+
---
8+
source:
9+
file: apiargs-common-param.yaml
10+
ref: $options
11+
...
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
source:
2+
file: apiargs-common-option.yaml
3+
ref: maxTimeMS
4+
---
5+
source:
6+
file: apiargs-MongoDBCollection-common-option.yaml
7+
ref: readConcern
8+
---
9+
source:
10+
file: apiargs-MongoDBCollection-common-option.yaml
11+
ref: readPreference
12+
---
13+
source:
14+
file: apiargs-common-option.yaml
15+
ref: session
16+
...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source:
2+
file: apiargs-common-param.yaml
3+
ref: $options
4+
...

docs/reference/class/MongoDBCollection.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Methods
6363
/reference/method/MongoDBCollection-aggregate
6464
/reference/method/MongoDBCollection-bulkWrite
6565
/reference/method/MongoDBCollection-count
66+
/reference/method/MongoDBCollection-countDocuments
6667
/reference/method/MongoDBCollection-createIndex
6768
/reference/method/MongoDBCollection-createIndexes
6869
/reference/method/MongoDBCollection-deleteMany
@@ -71,6 +72,7 @@ Methods
7172
/reference/method/MongoDBCollection-drop
7273
/reference/method/MongoDBCollection-dropIndex
7374
/reference/method/MongoDBCollection-dropIndexes
75+
/reference/method/MongoDBCollection-estimatedDocumentCount
7476
/reference/method/MongoDBCollection-explain
7577
/reference/method/MongoDBCollection-find
7678
/reference/method/MongoDBCollection-findOne
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
=====================================
2+
MongoDB\\Collection::countDocuments()
3+
=====================================
4+
5+
.. versionadded:: 1.4
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Collection::countDocuments()
19+
20+
Count the number of documents that match the filter criteria.
21+
22+
.. code-block:: php
23+
24+
function countDocuments($filter = [], array $options = []): integer
25+
26+
This method has the following parameters:
27+
28+
.. include:: /includes/apiargs/MongoDBCollection-method-countDocuments-param.rst
29+
30+
The ``$options`` parameter supports the following options:
31+
32+
.. include:: /includes/apiargs/MongoDBCollection-method-countDocuments-option.rst
33+
34+
Return Values
35+
-------------
36+
37+
The number of documents matching the filter criteria.
38+
39+
Errors/Exceptions
40+
-----------------
41+
42+
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
43+
.. include:: /includes/extracts/error-unsupportedexception.rst
44+
.. include:: /includes/extracts/error-invalidargumentexception.rst
45+
.. include:: /includes/extracts/error-driver-runtimeexception.rst
46+
47+
Behavior
48+
--------
49+
50+
.. include:: /includes/extracts/note-bson-comparison.rst
51+
52+
Internally, this method uses the ``$group`` aggregation pipeline operator to
53+
obtain the result. If a ``filter`` parameter is given, this is converted into
54+
a ``$match`` pipeline operator. Optional ``skip`` and ``limit`` stages are
55+
added between ``$match`` and ``group`` if present in the options.
56+
57+
.. todo: add output and examples
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=============================================
2+
MongoDB\\Collection::estimatedDocumentCount()
3+
=============================================
4+
5+
.. versionadded:: 1.4
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Collection::estimatedDocumentCount()
19+
20+
Gets an estimated number of documents in the collection using collection metadata.
21+
22+
.. code-block:: php
23+
24+
function countDocuments(array $options = []): integer
25+
26+
This method has the following parameters:
27+
28+
.. include:: /includes/apiargs/MongoDBCollection-method-estimateDocumentCount-param.rst
29+
30+
The ``$options`` parameter supports the following options:
31+
32+
.. include:: /includes/apiargs/MongoDBCollection-method-estimateDocumentCount-option.rst
33+
34+
Return Values
35+
-------------
36+
37+
An estimated number of documents in the collection.
38+
39+
Errors/Exceptions
40+
-----------------
41+
42+
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
43+
.. include:: /includes/extracts/error-unsupportedexception.rst
44+
.. include:: /includes/extracts/error-invalidargumentexception.rst
45+
.. include:: /includes/extracts/error-driver-runtimeexception.rst
46+
47+
Behavior
48+
--------
49+
50+
.. include:: /includes/extracts/note-bson-comparison.rst
51+
52+
See Also
53+
--------
54+
55+
- :manual:`count </reference/command/count>` command reference in the MongoDB
56+
manual

src/Collection.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
use MongoDB\Operation\BulkWrite;
3636
use MongoDB\Operation\CreateIndexes;
3737
use MongoDB\Operation\Count;
38+
use MongoDB\Operation\CountDocuments;
3839
use MongoDB\Operation\DeleteMany;
3940
use MongoDB\Operation\DeleteOne;
4041
use MongoDB\Operation\Distinct;
4142
use MongoDB\Operation\DropCollection;
4243
use MongoDB\Operation\DropIndexes;
44+
use MongoDB\Operation\EstimatedDocumentCount;
4345
use MongoDB\Operation\Explain;
4446
use MongoDB\Operation\Explainable;
4547
use MongoDB\Operation\Find;
@@ -255,6 +257,8 @@ public function bulkWrite(array $operations, array $options = [])
255257
* @throws UnsupportedException if options are not supported by the selected server
256258
* @throws InvalidArgumentException for parameter/option parsing errors
257259
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
260+
*
261+
* @deprecated 1.4
258262
*/
259263
public function count($filter = [], array $options = [])
260264
{
@@ -273,6 +277,35 @@ public function count($filter = [], array $options = [])
273277
return $operation->execute($server);
274278
}
275279

280+
/**
281+
* Gets the number of documents matching the filter.
282+
*
283+
* @see CountDocuments::__construct() for supported options
284+
* @param array|object $filter Query by which to filter documents
285+
* @param array $options Command options
286+
* @return integer
287+
* @throws UnexpectedValueException if the command response was malformed
288+
* @throws UnsupportedException if options are not supported by the selected server
289+
* @throws InvalidArgumentException for parameter/option parsing errors
290+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
291+
*/
292+
public function countDocuments($filter = [], array $options = [])
293+
{
294+
if ( ! isset($options['readPreference'])) {
295+
$options['readPreference'] = $this->readPreference;
296+
}
297+
298+
$server = $this->manager->selectServer($options['readPreference']);
299+
300+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
301+
$options['readConcern'] = $this->readConcern;
302+
}
303+
304+
$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);
305+
306+
return $operation->execute($server);
307+
}
308+
276309
/**
277310
* Create a single index for the collection.
278311
*
@@ -501,6 +534,34 @@ public function dropIndexes(array $options = [])
501534
return $operation->execute($server);
502535
}
503536

537+
/**
538+
* Gets an estimated number of documents in the collection using the collection metadata.
539+
*
540+
* @see EstimatedDocumentCount::__construct() for supported options
541+
* @param array $options Command options
542+
* @return integer
543+
* @throws UnexpectedValueException if the command response was malformed
544+
* @throws UnsupportedException if options are not supported by the selected server
545+
* @throws InvalidArgumentException for parameter/option parsing errors
546+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
547+
*/
548+
public function EstimatedDocumentCount(array $options = [])
549+
{
550+
if ( ! isset($options['readPreference'])) {
551+
$options['readPreference'] = $this->readPreference;
552+
}
553+
554+
$server = $this->manager->selectServer($options['readPreference']);
555+
556+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
557+
$options['readConcern'] = $this->readConcern;
558+
}
559+
560+
$operation = new EstimatedDocumentCount($this->databaseName, $this->collectionName, $options);
561+
562+
return $operation->execute($server);
563+
}
564+
504565
/**
505566
* Explains explainable commands.
506567
*

0 commit comments

Comments
 (0)