Skip to content

Commit 0ff26cd

Browse files
committed
PHPLIB-238: Implement Bucket::findOne() method
1 parent 78e6981 commit 0ff26cd

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
source:
2+
file: apiargs-MongoDBCollection-method-find-option.yaml
3+
ref: projection
4+
---
5+
source:
6+
file: apiargs-MongoDBCollection-method-find-option.yaml
7+
ref: sort
8+
---
9+
source:
10+
file: apiargs-MongoDBCollection-method-find-option.yaml
11+
ref: skip
12+
---
13+
source:
14+
file: apiargs-MongoDBCollection-common-option.yaml
15+
ref: collation
16+
---
17+
source:
18+
file: apiargs-MongoDBCollection-method-find-option.yaml
19+
ref: comment
20+
---
21+
source:
22+
file: apiargs-common-option.yaml
23+
ref: maxTimeMS
24+
---
25+
source:
26+
file: apiargs-MongoDBCollection-method-find-option.yaml
27+
ref: readConcern
28+
---
29+
source:
30+
file: apiargs-MongoDBGridFSBucket-method-find-option.yaml
31+
ref: readPreference
32+
---
33+
source:
34+
file: apiargs-MongoDBGridFSBucket-method-find-option.yaml
35+
ref: typeMap
36+
post: |
37+
This will be used for the returned result document.
38+
---
39+
source:
40+
file: apiargs-MongoDBCollection-method-find-option.yaml
41+
ref: modifiers
42+
...

docs/reference/class/MongoDBGridFSBucket.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Methods
4040
/reference/method/MongoDBGridFSBucket-downloadToStreamByName
4141
/reference/method/MongoDBGridFSBucket-drop
4242
/reference/method/MongoDBGridFSBucket-find
43+
/reference/method/MongoDBGridFSBucket-findOne
4344
/reference/method/MongoDBGridFSBucket-getBucketName
4445
/reference/method/MongoDBGridFSBucket-getDatabaseName
4546
/reference/method/MongoDBGridFSBucket-getFileDocumentForStream

docs/reference/method/MongoDBGridFSBucket-find.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ See Also
4040
--------
4141

4242
- :phpmethod:`MongoDB\\Collection::find()`
43+
- :phpmethod:`MongoDB\\GridFS\\Bucket::findOne()`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
==================================
2+
MongoDB\\GridFS\\Bucket::findOne()
3+
==================================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. phpmethod:: MongoDB\\GridFS\\Bucket::findOne()
17+
18+
Finds a single document from the GridFS bucket's files collection matching
19+
the query.
20+
21+
.. code-block:: php
22+
23+
function findOne($filter = [], array $options = []): array|object|null
24+
25+
This method has the following parameters:
26+
27+
.. include:: /includes/apiargs/MongoDBCollection-method-findOne-param.rst
28+
29+
The ``$options`` parameter supports the following options:
30+
31+
.. include:: /includes/apiargs/MongoDBGridFSBucket-method-findOne-option.rst
32+
33+
Return Values
34+
-------------
35+
36+
An array or object for the :term:`first document <natural order>` that matched
37+
the query, or ``null`` if no document matched the query. The return type will
38+
depend on the ``typeMap`` option.
39+
40+
.. todo: add examples
41+
42+
See Also
43+
--------
44+
45+
- :phpmethod:`MongoDB\\Collection::findOne()`
46+
- :phpmethod:`MongoDB\\GridFS\\Bucket::find()`

src/GridFS/Bucket.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ public function find($filter = [], array $options = [])
223223
return $this->collectionWrapper->findFiles($filter, $options);
224224
}
225225

226+
/**
227+
* Finds a single document from the GridFS bucket's files collection
228+
* matching the query.
229+
*
230+
* @see FindOne::__construct() for supported options
231+
* @param array|object $filter Query by which to filter documents
232+
* @param array $options Additional options
233+
* @return array|object|null
234+
*/
235+
public function findOne($filter = [], array $options = [])
236+
{
237+
return $this->collectionWrapper->findOneFile($filter, $options);
238+
}
239+
226240
/**
227241
* Return the bucket name.
228242
*

src/GridFS/CollectionWrapper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ public function findFiles($filter, array $options = [])
140140
return $this->filesCollection->find($filter, $options);
141141
}
142142

143+
/**
144+
* Finds a single document from the GridFS bucket's files collection.
145+
*
146+
* @param array|object $filter Query by which to filter documents
147+
* @param array $options Additional options
148+
* @return array|object|null
149+
*/
150+
public function findOneFile($filter, array $options = [])
151+
{
152+
return $this->filesCollection->findOne($filter, $options);
153+
}
154+
143155
/**
144156
* Return the bucket name.
145157
*

tests/GridFS/BucketFunctionalTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,28 @@ public function testFindUsesTypeMap()
328328
$this->assertInstanceOf('MongoDB\Model\BSONDocument', $fileDocument);
329329
}
330330

331+
public function testFindOne()
332+
{
333+
$this->bucket->uploadFromStream('a', $this->createStream('foo'));
334+
$this->bucket->uploadFromStream('b', $this->createStream('foobar'));
335+
$this->bucket->uploadFromStream('c', $this->createStream('foobarbaz'));
336+
337+
$fileDocument = $this->bucket->findOne(
338+
['length' => ['$lte' => 6]],
339+
[
340+
'projection' => [
341+
'filename' => 1,
342+
'length' => 1,
343+
'_id' => 0,
344+
],
345+
'sort' => ['length' => -1],
346+
]
347+
);
348+
349+
$this->assertInstanceOf('MongoDB\Model\BSONDocument', $fileDocument);
350+
$this->assertSameDocument(['filename' => 'b', 'length' => 6], $fileDocument);
351+
}
352+
331353
public function testGetBucketNameWithCustomValue()
332354
{
333355
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']);

0 commit comments

Comments
 (0)