首页 > 解决方案 > 如何使用“新的 MongoDB\Driver\Manager”驱动程序在 php 中获取集合 {Mongodb} 的列表

问题描述

我想使用 php 获取 monogoDB 的所有集合。

我使用下面的代码只得到一个集合。

<?php
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$listdatabases = new MongoDB\Driver\Command(["listCollections" => 1]);
$res = $manager->executeCommand("mydatabasename", $listdatabases);
$collections = current($res->toArray());
print_r($collections);

stdClass Object
(
    [name] => collectionname
    [type] => collection
    [options] => stdClass Object
        (
        )

    [info] => stdClass Object
        (
            [readOnly] => 
            [uuid] => MongoDB\BSON\Binary Object
                (
                    [data] => �����B���tqIB
                    [type] => 4
                )

        )

    [idIndex] => stdClass Object
        (
            [v] => 2
            [key] => stdClass Object
                (
                    [_id] => 1
                )

            [name] => _id_
            [ns] => collectionname
        )

)

但我想要所有收藏列表。

MongoDB 版本:3.4.16

php版本:7.1

标签: phpmongodb

解决方案


你只需要从

$collections = current($res->toArray());

您将获得所有收藏品。它应该是

$collections = $res->toArray();

推荐阅读