Skip to content

Commit 1cf6add

Browse files
committed
Add example for querying with BSON classes in findOne() docs
1 parent 6993005 commit 1cf6add

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/reference/method/MongoDBCollection-findOne.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ Behavior
5151
Examples
5252
--------
5353

54+
Matching BSON Types in Query Criteria
55+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56+
57+
In the following example, documents in the ``restaurants`` collection use an
58+
:manual:`ObjectId </reference/object-id/>` for their identifier (the default)
59+
and documents in the ``zips`` collection use a string. Since ObjectID is a
60+
special BSON type, the query criteria for selecting a restaurant must use the
61+
:php:`MongoDB\\BSON\\ObjectID <class.mongodb-bson-objectid>` class.
62+
63+
.. code-block:: php
64+
65+
$database = (new MongoDB\Client)->test;
66+
67+
$zip = $database->zips->findOne(['_id' => '10036']);
68+
69+
$restaurant = $database->restaurants->findOne([
70+
'_id' => new MongoDB\BSON\ObjectID('594d5ef280a846852a4b3f70'),
71+
])
72+
73+
Projecting Fields
74+
~~~~~~~~~~~~~~~~~
75+
5476
The following example finds a restaurant based on the ``cuisine`` and
5577
``borough`` fields and uses a :manual:`projection
5678
</tutorial/project-fields-from-query-results>` to limit the fields that are

0 commit comments

Comments
 (0)