ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs 菜单

MongoDB\Database::selectCollection()

MongoDB\Database::selectCollection()

Selects a collection within the database. This method is aliased by MongoDB\Database::getCollection() and will be replaced by it in a future release.

function selectCollection(
string $collectionName,
array $options = []
): MongoDB\Collection
$collectionName : 细绳
要选择的collection的名称。
$options : array

指定所需选项的数组。

名称
类型
说明

编解码器

MongoDB\Codec\DocumentCodec

用于集合操作的默认编解码器

1.17 版本中的新增功能

事务外的

用于collection操作的默认读关注(read concern)。默认为数据库的读关注(read concern)。

readPreference

用于collection操作的默认读取偏好(read preference)。默认为数据库的读取偏好(read preference)。

typeMap

阵列

用于collection操作的默认类型映射。默认为数据库的类型映射。

writeConcern

用于collection操作的默认写关注(write concern)。默认为数据库的写关注(write concern)。

一个MongoDB\Collection对象。

MongoDB\Exception\InvalidArgumentException表示与参数或选项解析相关的错误。

所选集合会从 Database 对象继承读取偏好(read preference)和类型映射等选项。可以使用 $options 参数覆盖选项。

userstest以下示例选择数据库中的collection集合:

<?php
$db = (new MongoDB\Client)->test;
$collection = $db->selectCollection('users');

以下示例使用自定义读取偏好(readusers testpreference)选择数据库中的collection集合:

<?php
$db = (new MongoDB\Client)->test;
$users = $db->selectCollection(
'users',
[
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]
);