-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMongoDBClient-selectCollection.txt
129 lines (91 loc) · 3.16 KB
/
MongoDBClient-selectCollection.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
===================================
MongoDB\\Client::selectCollection()
===================================
.. meta::
:description: Select a collection on the server with the MongoDB PHP Library, with customizable options for read and write concerns.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Client::selectCollection()
Selects a collection on the server. This method is aliased by
:phpmethod:`MongoDB\Client::getCollection()` and will be replaced by
it in a future release.
.. code-block:: php
function selectCollection(
string $databaseName,
string $collectionName,
array $options = []
): MongoDB\Collection
Parameters
----------
``$databaseName`` : string
The name of the database containing the collection to select.
``$collectionName`` : string
The name of the collection to select.
``$options`` : array
An array specifying the desired options.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - codec
- MongoDB\\Codec\\DocumentCodec
- The default :doc:`codec </data-formats/codecs>` to use for collection
operations.
.. versionadded:: 1.17
* - readConcern
- :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>`
- The default read concern to use for collection operations. Defaults to
the client's read concern.
* - readPreference
- :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>`
- The default read preference to use for collection operations. Defaults
to the client's read preference.
* - typeMap
- array
- The default type map to use for collection operations. Defaults to the
client's type map.
* - writeConcern
- :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
- The default write concern to use for collection operations. Defaults to
the client's write concern.
Return Values
-------------
A :phpclass:`MongoDB\Collection` object.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-invalidargumentexception.rst
Behavior
--------
The selected collection inherits options such as read preference and type
mapping from the :phpclass:`Client <MongoDB\Client>` object. Options may be
overridden by using the ``$options`` parameter.
Example
-------
The following example selects the ``users`` collection in the ``test`` database:
.. code-block:: php
<?php
$client = new MongoDB\Client;
$collection = $client->selectCollection('test', 'users');
The following example selects the ``users`` collection in the ``test`` database
with a custom read preference:
.. code-block:: php
<?php
$client = new MongoDB\Client;
$collection = $client->selectCollection(
'test',
'users',
[
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]
);
See Also
--------
- :phpmethod:`MongoDB\Collection::__construct()`
- :phpmethod:`MongoDB\Database::selectCollection()`