Commit 0ff26cd8 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-238: Implement Bucket::findOne() method

parent 78e69811
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: projection
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: sort
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: skip
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: comment
---
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: readConcern
---
source:
file: apiargs-MongoDBGridFSBucket-method-find-option.yaml
ref: readPreference
---
source:
file: apiargs-MongoDBGridFSBucket-method-find-option.yaml
ref: typeMap
post: |
This will be used for the returned result document.
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: modifiers
...
...@@ -40,6 +40,7 @@ Methods ...@@ -40,6 +40,7 @@ Methods
/reference/method/MongoDBGridFSBucket-downloadToStreamByName /reference/method/MongoDBGridFSBucket-downloadToStreamByName
/reference/method/MongoDBGridFSBucket-drop /reference/method/MongoDBGridFSBucket-drop
/reference/method/MongoDBGridFSBucket-find /reference/method/MongoDBGridFSBucket-find
/reference/method/MongoDBGridFSBucket-findOne
/reference/method/MongoDBGridFSBucket-getBucketName /reference/method/MongoDBGridFSBucket-getBucketName
/reference/method/MongoDBGridFSBucket-getDatabaseName /reference/method/MongoDBGridFSBucket-getDatabaseName
/reference/method/MongoDBGridFSBucket-getFileDocumentForStream /reference/method/MongoDBGridFSBucket-getFileDocumentForStream
......
...@@ -40,3 +40,4 @@ See Also ...@@ -40,3 +40,4 @@ See Also
-------- --------
- :phpmethod:`MongoDB\\Collection::find()` - :phpmethod:`MongoDB\\Collection::find()`
- :phpmethod:`MongoDB\\GridFS\\Bucket::findOne()`
==================================
MongoDB\\GridFS\\Bucket::findOne()
==================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\GridFS\\Bucket::findOne()
Finds a single document from the GridFS bucket's files collection matching
the query.
.. code-block:: php
function findOne($filter = [], array $options = []): array|object|null
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBCollection-method-findOne-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBGridFSBucket-method-findOne-option.rst
Return Values
-------------
An array or object for the :term:`first document <natural order>` that matched
the query, or ``null`` if no document matched the query. The return type will
depend on the ``typeMap`` option.
.. todo: add examples
See Also
--------
- :phpmethod:`MongoDB\\Collection::findOne()`
- :phpmethod:`MongoDB\\GridFS\\Bucket::find()`
...@@ -223,6 +223,20 @@ class Bucket ...@@ -223,6 +223,20 @@ class Bucket
return $this->collectionWrapper->findFiles($filter, $options); return $this->collectionWrapper->findFiles($filter, $options);
} }
/**
* Finds a single document from the GridFS bucket's files collection
* matching the query.
*
* @see FindOne::__construct() for supported options
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return array|object|null
*/
public function findOne($filter = [], array $options = [])
{
return $this->collectionWrapper->findOneFile($filter, $options);
}
/** /**
* Return the bucket name. * Return the bucket name.
* *
......
...@@ -140,6 +140,18 @@ class CollectionWrapper ...@@ -140,6 +140,18 @@ class CollectionWrapper
return $this->filesCollection->find($filter, $options); return $this->filesCollection->find($filter, $options);
} }
/**
* Finds a single document from the GridFS bucket's files collection.
*
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return array|object|null
*/
public function findOneFile($filter, array $options = [])
{
return $this->filesCollection->findOne($filter, $options);
}
/** /**
* Return the bucket name. * Return the bucket name.
* *
......
...@@ -328,6 +328,28 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -328,6 +328,28 @@ class BucketFunctionalTest extends FunctionalTestCase
$this->assertInstanceOf('MongoDB\Model\BSONDocument', $fileDocument); $this->assertInstanceOf('MongoDB\Model\BSONDocument', $fileDocument);
} }
public function testFindOne()
{
$this->bucket->uploadFromStream('a', $this->createStream('foo'));
$this->bucket->uploadFromStream('b', $this->createStream('foobar'));
$this->bucket->uploadFromStream('c', $this->createStream('foobarbaz'));
$fileDocument = $this->bucket->findOne(
['length' => ['$lte' => 6]],
[
'projection' => [
'filename' => 1,
'length' => 1,
'_id' => 0,
],
'sort' => ['length' => -1],
]
);
$this->assertInstanceOf('MongoDB\Model\BSONDocument', $fileDocument);
$this->assertSameDocument(['filename' => 'b', 'length' => 6], $fileDocument);
}
public function testGetBucketNameWithCustomValue() public function testGetBucketNameWithCustomValue()
{ {
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']); $bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment