Docs Menu

Docs HomePHP Library Manual

MongoDB\Collection::withOptions()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Example
  • See Also
MongoDB\Collection::withOptions()

Returns a clone of the Collection object, but with different options.

function withOptions(array $options = []): MongoDB\Collection
$options : array

An array specifying the desired options.

Name
Type
Description
codec
MongoDB\Codec\DocumentCodec

The default codec to use for collection operations. Defaults to the original collection's codec.

New in version 1.17.

readConcern
MongoDB\Driver\ReadConcern
The default read concern to use for collection operations. Defaults to the original collection's read concern.
readPreference
The default read preference to use for collection operations. Defaults to the original collection's read preference.
typeMap
array
The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the original collection's type map.
writeConcern
The default write concern to use for collection operations. Defaults to the original collection's write concern.

A MongoDB\Collection object.

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

The following example clones an existing Collection object with a new read preference:

<?php
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
$newCollection = $sourceCollection->withOptions([
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]);
← MongoDB\Collection::watch()