MongoDB\Collection::withOptions()
Definition
Parameters
$options
: arrayAn array specifying the desired options.
NameTypeDescriptioncodecMongoDB\Codec\DocumentCodecThe default codec to use for collection operations. Defaults to the original collection's codec.
New in version 1.17.
readConcernThe default read concern to use for collection operations. Defaults to the original collection's read concern.readPreferenceThe default read preference to use for collection operations. Defaults to the original collection's read preference.typeMaparrayThe type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the original collection's type map.writeConcernThe default write concern to use for collection operations. Defaults to the original collection's write concern.
Return Values
A MongoDB\Collection
object.
Errors/Exceptions
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
Example
The following example clones an existing Collection object with a new read preference:
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants'); $newCollection = $sourceCollection->withOptions([ 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), ]);