Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
9f2700fb
Commit
9f2700fb
authored
Jun 21, 2018
by
Derick Rethans
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-354: Implement new count API
parent
71d76b15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
851 additions
and
4 deletions
+851
-4
apiargs-MongoDBCollection-method-countDocuments-option.yaml
...iargs-MongoDBCollection-method-countDocuments-option.yaml
+49
-0
apiargs-MongoDBCollection-method-countDocuments-param.yaml
...piargs-MongoDBCollection-method-countDocuments-param.yaml
+11
-0
apiargs-MongoDBCollection-method-estimateDocumentCount-option.yaml
...ongoDBCollection-method-estimateDocumentCount-option.yaml
+16
-0
apiargs-MongoDBCollection-method-estimateDocumentCount-param.yaml
...MongoDBCollection-method-estimateDocumentCount-param.yaml
+4
-0
MongoDBCollection.txt
docs/reference/class/MongoDBCollection.txt
+2
-0
MongoDBCollection-countDocuments.txt
docs/reference/method/MongoDBCollection-countDocuments.txt
+57
-0
MongoDBCollection-estimatedDocumentCount.txt
...rence/method/MongoDBCollection-estimatedDocumentCount.txt
+56
-0
Collection.php
src/Collection.php
+61
-0
CountDocuments.php
src/Operation/CountDocuments.php
+230
-0
EstimatedDocumentCount.php
src/Operation/EstimatedDocumentCount.php
+173
-0
CrudSpecFunctionalTest.php
tests/Collection/CrudSpecFunctionalTest.php
+6
-0
count-collation.json
tests/Collection/spec-tests/read/count-collation.json
+19
-1
count.json
tests/Collection/spec-tests/read/count.json
+55
-3
CountDocumentsTest.php
tests/Operation/CountDocumentsTest.php
+71
-0
EstimatedDocumentCountTest.php
tests/Operation/EstimatedDocumentCountTest.php
+41
-0
No files found.
docs/includes/apiargs-MongoDBCollection-method-countDocuments-option.yaml
0 → 100644
View file @
9f2700fb
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
collation
---
arg_name
:
option
name
:
hint
type
:
string|array|object
description
:
|
The index to use. Specify either the index name as a string or the index key
pattern as a document. If specified, then the query system will only consider
plans using the hinted index.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
limit
type
:
integer
description
:
|
The maximum number of matching documents to return.
interface
:
phpmethod
operation
:
~
optional
:
true
---
source
:
file
:
apiargs-common-option.yaml
ref
:
maxTimeMS
---
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
readConcern
---
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
readPreference
---
source
:
file
:
apiargs-common-option.yaml
ref
:
session
---
arg_name
:
option
name
:
skip
type
:
integer
description
:
|
The number of matching documents to skip before returning results.
interface
:
phpmethod
operation
:
~
optional
:
true
...
docs/includes/apiargs-MongoDBCollection-method-countDocuments-param.yaml
0 → 100644
View file @
9f2700fb
source
:
file
:
apiargs-MongoDBCollection-common-param.yaml
ref
:
$filter
optional
:
true
replacement
:
action
:
"
to
count"
---
source
:
file
:
apiargs-common-param.yaml
ref
:
$options
...
docs/includes/apiargs-MongoDBCollection-method-estimateDocumentCount-option.yaml
0 → 100644
View file @
9f2700fb
source
:
file
:
apiargs-common-option.yaml
ref
:
maxTimeMS
---
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
readConcern
---
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
readPreference
---
source
:
file
:
apiargs-common-option.yaml
ref
:
session
...
docs/includes/apiargs-MongoDBCollection-method-estimateDocumentCount-param.yaml
0 → 100644
View file @
9f2700fb
source
:
file
:
apiargs-common-param.yaml
ref
:
$options
...
docs/reference/class/MongoDBCollection.txt
View file @
9f2700fb
...
...
@@ -63,6 +63,7 @@ Methods
/reference/method/MongoDBCollection-aggregate
/reference/method/MongoDBCollection-bulkWrite
/reference/method/MongoDBCollection-count
/reference/method/MongoDBCollection-countDocuments
/reference/method/MongoDBCollection-createIndex
/reference/method/MongoDBCollection-createIndexes
/reference/method/MongoDBCollection-deleteMany
...
...
@@ -71,6 +72,7 @@ Methods
/reference/method/MongoDBCollection-drop
/reference/method/MongoDBCollection-dropIndex
/reference/method/MongoDBCollection-dropIndexes
/reference/method/MongoDBCollection-estimatedDocumentCount
/reference/method/MongoDBCollection-explain
/reference/method/MongoDBCollection-find
/reference/method/MongoDBCollection-findOne
...
...
docs/reference/method/MongoDBCollection-countDocuments.txt
0 → 100644
View file @
9f2700fb
=====================================
MongoDB\\Collection::countDocuments()
=====================================
.. versionadded:: 1.4
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\Collection::countDocuments()
Count the number of documents that match the filter criteria.
.. code-block:: php
function countDocuments($filter = [], array $options = []): integer
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBCollection-method-countDocuments-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBCollection-method-countDocuments-option.rst
Return Values
-------------
The number of documents matching the filter criteria.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Behavior
--------
.. include:: /includes/extracts/note-bson-comparison.rst
Internally, this method uses the ``$group`` aggregation pipeline operator to
obtain the result. If a ``filter`` parameter is given, this is converted into
a ``$match`` pipeline operator. Optional ``skip`` and ``limit`` stages are
added between ``$match`` and ``group`` if present in the options.
.. todo: add output and examples
docs/reference/method/MongoDBCollection-estimatedDocumentCount.txt
0 → 100644
View file @
9f2700fb
=============================================
MongoDB\\Collection::estimatedDocumentCount()
=============================================
.. versionadded:: 1.4
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\Collection::estimatedDocumentCount()
Gets an estimated number of documents in the collection using collection metadata.
.. code-block:: php
function countDocuments(array $options = []): integer
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBCollection-method-estimateDocumentCount-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBCollection-method-estimateDocumentCount-option.rst
Return Values
-------------
An estimated number of documents in the collection.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Behavior
--------
.. include:: /includes/extracts/note-bson-comparison.rst
See Also
--------
- :manual:`count </reference/command/count>` command reference in the MongoDB
manual
src/Collection.php
View file @
9f2700fb
...
...
@@ -35,11 +35,13 @@ use MongoDB\Operation\Aggregate;
use
MongoDB\Operation\BulkWrite
;
use
MongoDB\Operation\CreateIndexes
;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\CountDocuments
;
use
MongoDB\Operation\DeleteMany
;
use
MongoDB\Operation\DeleteOne
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropIndexes
;
use
MongoDB\Operation\EstimatedDocumentCount
;
use
MongoDB\Operation\Explain
;
use
MongoDB\Operation\Explainable
;
use
MongoDB\Operation\Find
;
...
...
@@ -255,6 +257,8 @@ class Collection
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*
* @deprecated 1.4
*/
public
function
count
(
$filter
=
[],
array
$options
=
[])
{
...
...
@@ -273,6 +277,35 @@ class Collection
return
$operation
->
execute
(
$server
);
}
/**
* Gets the number of documents matching the filter.
*
* @see CountDocuments::__construct() for supported options
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
countDocuments
(
$filter
=
[],
array
$options
=
[])
{
if
(
!
isset
(
$options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
readPreference
;
}
$server
=
$this
->
manager
->
selectServer
(
$options
[
'readPreference'
]);
if
(
!
isset
(
$options
[
'readConcern'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
$options
[
'readConcern'
]
=
$this
->
readConcern
;
}
$operation
=
new
CountDocuments
(
$this
->
databaseName
,
$this
->
collectionName
,
$filter
,
$options
);
return
$operation
->
execute
(
$server
);
}
/**
* Create a single index for the collection.
*
...
...
@@ -501,6 +534,34 @@ class Collection
return
$operation
->
execute
(
$server
);
}
/**
* Gets an estimated number of documents in the collection using the collection metadata.
*
* @see EstimatedDocumentCount::__construct() for supported options
* @param array $options Command options
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
EstimatedDocumentCount
(
array
$options
=
[])
{
if
(
!
isset
(
$options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
readPreference
;
}
$server
=
$this
->
manager
->
selectServer
(
$options
[
'readPreference'
]);
if
(
!
isset
(
$options
[
'readConcern'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
$options
[
'readConcern'
]
=
$this
->
readConcern
;
}
$operation
=
new
EstimatedDocumentCount
(
$this
->
databaseName
,
$this
->
collectionName
,
$options
);
return
$operation
->
execute
(
$server
);
}
/**
* Explains explainable commands.
*
...
...
src/Operation/CountDocuments.php
0 → 100644
View file @
9f2700fb
<?php
/*
* Copyright 2015-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Session
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnexpectedValueException
;
use
MongoDB\Exception\UnsupportedException
;
/**
* Operation for obtaining an exact count of documents in a collection
*
* @api
* @see \MongoDB\Collection::countDocuments()
* @see https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#countdocuments
*/
class
CountDocuments
implements
Executable
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
$databaseName
;
private
$collectionName
;
private
$filter
;
private
$options
;
/**
* Constructs an aggregate command for counting documents
*
* Supported options:
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * hint (string|document): The index to use. Specify either the index
* name as a string or the index key pattern as a document. If specified,
* then the query system will only consider plans using the hinted index.
*
* * limit (integer): The maximum number of documents to count.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * skip (integer): The number of documents to skip before returning the
* documents.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
$filter
,
array
$options
=
[])
{
if
(
!
is_array
(
$filter
)
&&
!
is_object
(
$filter
))
{
throw
InvalidArgumentException
::
invalidType
(
'$filter'
,
$filter
,
'array or object'
);
}
if
(
isset
(
$options
[
'collation'
])
&&
!
is_array
(
$options
[
'collation'
])
&&
!
is_object
(
$options
[
'collation'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"collation" option'
,
$options
[
'collation'
],
'array or object'
);
}
if
(
isset
(
$options
[
'hint'
])
&&
!
is_string
(
$options
[
'hint'
])
&&
!
is_array
(
$options
[
'hint'
])
&&
!
is_object
(
$options
[
'hint'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"hint" option'
,
$options
[
'hint'
],
'string or array or object'
);
}
if
(
isset
(
$options
[
'limit'
])
&&
!
is_integer
(
$options
[
'limit'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"limit" option'
,
$options
[
'limit'
],
'integer'
);
}
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
'MongoDB\Driver\ReadConcern'
);
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
'MongoDB\Driver\Session'
);
}
if
(
isset
(
$options
[
'skip'
])
&&
!
is_integer
(
$options
[
'skip'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"skip" option'
,
$options
[
'skip'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
$options
[
'readConcern'
]
->
isDefault
())
{
unset
(
$options
[
'readConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
filter
=
$filter
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
execute
(
Server
$server
)
{
if
(
isset
(
$this
->
options
[
'collation'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCollation
))
{
throw
UnsupportedException
::
collationNotSupported
();
}
if
(
isset
(
$this
->
options
[
'readConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
throw
UnsupportedException
::
readConcernNotSupported
();
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
return
(
integer
)
$result
->
n
;
}
/**
* Create the count command document.
*
* @return array
*/
private
function
createCommandDocument
()
{
$pipeline
=
[
[
'$match'
=>
(
object
)
$this
->
filter
]
];
if
(
isset
(
$this
->
options
[
'skip'
]))
{
$pipeline
[]
=
[
'$skip'
=>
$this
->
options
[
'skip'
]];
}
if
(
isset
(
$this
->
options
[
'limit'
]))
{
$pipeline
[]
=
[
'$limit'
=>
$this
->
options
[
'limit'
]];
}
$pipeline
[]
=
[
'$group'
=>
[
'_id'
=>
null
,
'n'
=>
[
'$sum'
=>
1
]]];
$cmd
=
[
'aggregate'
=>
$this
->
collectionName
,
'pipeline'
=>
$pipeline
,
'cursor'
=>
(
object
)
[],
];
if
(
isset
(
$this
->
options
[
'collation'
]))
{
$cmd
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
}
if
(
isset
(
$this
->
options
[
'hint'
]))
{
$cmd
[
'hint'
]
=
is_array
(
$this
->
options
[
'hint'
])
?
(
object
)
$this
->
options
[
'hint'
]
:
$this
->
options
[
'hint'
];
}
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
return
$cmd
;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'readConcern'
]))
{
$options
[
'readConcern'
]
=
$this
->
options
[
'readConcern'
];
}
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
options
[
'readPreference'
];
}
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
return
$options
;
}
}
src/Operation/EstimatedDocumentCount.php
0 → 100644
View file @
9f2700fb
<?php
/*
* Copyright 2015-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Session
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnexpectedValueException
;
use
MongoDB\Exception\UnsupportedException
;
/**
* Operation for obtaining an estimated count of documents in a collection
*
* @api
* @see \MongoDB\Collection::estimatedDocumentCount()
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
class
EstimatedDocumentCount
implements
Executable
,
Explainable
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
$databaseName
;
private
$collectionName
;
private
$options
;
/**
* Constructs a count command.
*
* Supported options:
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
[])
{
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
'MongoDB\Driver\ReadConcern'
);
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
'MongoDB\Driver\Session'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
$options
[
'readConcern'
]
->
isDefault
())
{
unset
(
$options
[
'readConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
execute
(
Server
$server
)
{
if
(
isset
(
$this
->
options
[
'collation'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCollation
))
{
throw
UnsupportedException
::
collationNotSupported
();
}
if
(
isset
(
$this
->
options
[
'readConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
throw
UnsupportedException
::
readConcernNotSupported
();
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
// Older server versions may return a float
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
return
(
integer
)
$result
->
n
;
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
createCommandDocument
();
}
/**
* Create the count command document.
*
* @return array
*/
private
function
createCommandDocument
()
{
$cmd
=
[
'count'
=>
$this
->
collectionName
];
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
return
$cmd
;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'readConcern'
]))
{
$options
[
'readConcern'
]
=
$this
->
options
[
'readConcern'
];
}
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
options
[
'readPreference'
];
}
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
return
$options
;
}
}
tests/Collection/CrudSpecFunctionalTest.php
View file @
9f2700fb
...
...
@@ -123,12 +123,16 @@ class CrudSpecFunctionalTest extends FunctionalTestCase
);
case
'count'
:
case
'countDocuments'
:
case
'find'
:
return
$this
->
collection
->
{
$operation
[
'name'
]}(
isset
(
$operation
[
'arguments'
][
'filter'
])
?
$operation
[
'arguments'
][
'filter'
]
:
[],
array_diff_key
(
$operation
[
'arguments'
],
[
'filter'
=>
1
])
);
case
'estimatedDocumentCount'
:
return
$this
->
collection
->
estimatedDocumentCount
(
$operation
[
'arguments'
]);
case
'deleteMany'
:
case
'deleteOne'
:
case
'findOneAndDelete'
:
...
...
@@ -264,6 +268,8 @@ class CrudSpecFunctionalTest extends FunctionalTestCase
break
;
case
'count'
:
case
'countDocuments'
:
case
'estimatedDocumentCount'
:
$this
->
assertSame
(
$expectedResult
,
$actualResult
);
break
;
...
...
tests/Collection/spec-tests/read/count-collation.json
View file @
9f2700fb
...
...
@@ -8,7 +8,25 @@
"minServerVersion"
:
"3.4"
,
"tests"
:
[
{
"description"
:
"Count with collation"
,
"description"
:
"Count documents with collation"
,
"operation"
:
{
"name"
:
"countDocuments"
,
"arguments"
:
{
"filter"
:
{
"x"
:
"ping"
},
"collation"
:
{
"locale"
:
"en_US"
,
"strength"
:
2
}
}
},
"outcome"
:
{
"result"
:
1
}
},
{
"description"
:
"Deprecated count with collation"
,
"operation"
:
{
"name"
:
"count"
,
"arguments"
:
{
...
...
tests/Collection/spec-tests/read/count.json
View file @
9f2700fb
...
...
@@ -15,7 +15,59 @@
],
"tests"
:
[
{
"description"
:
"Count without a filter"
,
"description"
:
"Estimated document count"
,
"operation"
:
{
"name"
:
"estimatedDocumentCount"
,
"arguments"
:
{}
},
"outcome"
:
{
"result"
:
3
}
},
{
"description"
:
"Count documents without a filter"
,
"operation"
:
{
"name"
:
"countDocuments"
,
"arguments"
:
{
"filter"
:
{}
}
},
"outcome"
:
{
"result"
:
3
}
},
{
"description"
:
"Count documents with a filter"
,
"operation"
:
{
"name"
:
"countDocuments"
,
"arguments"
:
{
"filter"
:
{
"_id"
:
{
"$gt"
:
1
}
}
}
},
"outcome"
:
{
"result"
:
2
}
},
{
"description"
:
"Count documents with skip and limit"
,
"operation"
:
{
"name"
:
"countDocuments"
,
"arguments"
:
{
"filter"
:
{},
"skip"
:
1
,
"limit"
:
3
}
},
"outcome"
:
{
"result"
:
2
}
},
{
"description"
:
"Deprecated count without a filter"
,
"operation"
:
{
"name"
:
"count"
,
"arguments"
:
{
...
...
@@ -27,7 +79,7 @@
}
},
{
"description"
:
"
C
ount with a filter"
,
"description"
:
"
Deprecated c
ount with a filter"
,
"operation"
:
{
"name"
:
"count"
,
"arguments"
:
{
...
...
@@ -43,7 +95,7 @@
}
},
{
"description"
:
"
C
ount with skip and limit"
,
"description"
:
"
Deprecated c
ount with skip and limit"
,
"operation"
:
{
"name"
:
"count"
,
"arguments"
:
{
...
...
tests/Operation/CountDocumentsTest.php
0 → 100644
View file @
9f2700fb
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Operation\CountDocuments
;
class
CountDocumentsTest
extends
TestCase
{
/**
* @dataProvider provideInvalidDocumentValues
*/
public
function
testConstructorFilterArgumentTypeCheck
(
$filter
)
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
new
CountDocuments
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
);
}
/**
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
new
CountDocuments
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidDocumentValues
()
as
$value
)
{
$options
[][]
=
[
'collation'
=>
$value
];
}
foreach
(
$this
->
getInvalidHintValues
()
as
$value
)
{
$options
[][]
=
[
'hint'
=>
$value
];
}
foreach
(
$this
->
getInvalidIntegerValues
()
as
$value
)
{
$options
[][]
=
[
'limit'
=>
$value
];
}
foreach
(
$this
->
getInvalidIntegerValues
()
as
$value
)
{
$options
[][]
=
[
'maxTimeMS'
=>
$value
];
}
foreach
(
$this
->
getInvalidReadConcernValues
()
as
$value
)
{
$options
[][]
=
[
'readConcern'
=>
$value
];
}
foreach
(
$this
->
getInvalidReadPreferenceValues
()
as
$value
)
{
$options
[][]
=
[
'readPreference'
=>
$value
];
}
foreach
(
$this
->
getInvalidSessionValues
()
as
$value
)
{
$options
[][]
=
[
'session'
=>
$value
];
}
foreach
(
$this
->
getInvalidIntegerValues
()
as
$value
)
{
$options
[][]
=
[
'skip'
=>
$value
];
}
return
$options
;
}
private
function
getInvalidHintValues
()
{
return
[
123
,
3.14
,
true
];
}
}
tests/Operation/EstimatedDocumentCountTest.php
0 → 100644
View file @
9f2700fb
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Operation\EstimatedDocumentCount
;
class
EstimatedDocumentCountTest
extends
TestCase
{
/**
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
new
EstimatedDocumentCount
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidIntegerValues
()
as
$value
)
{
$options
[][]
=
[
'maxTimeMS'
=>
$value
];
}
foreach
(
$this
->
getInvalidReadConcernValues
()
as
$value
)
{
$options
[][]
=
[
'readConcern'
=>
$value
];
}
foreach
(
$this
->
getInvalidReadPreferenceValues
()
as
$value
)
{
$options
[][]
=
[
'readPreference'
=>
$value
];
}
foreach
(
$this
->
getInvalidSessionValues
()
as
$value
)
{
$options
[][]
=
[
'session'
=>
$value
];
}
return
$options
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment