php - 如何使用“新的 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
解决方案
你只需要从
$collections = current($res->toArray());
您将获得所有收藏品。它应该是
$collections = $res->toArray();
推荐阅读
- javascript - 本地存储中的数据附加到 HTML
- button - SwiftUI 按钮无法识别按钮的框架
- android - Android Studio Kotlin 蓝牙重连问题
- javascript - 如何在 mocha 和 chai 测试中导出 Solidity 函数?
- python - 如何获取打印到终端的所有信息,包括错误
- flutter - 使用 Stack 小部件时如何防止自定义小部件的默认样式被更改?
- javascript - 获取 Cors 策略错误本地主机无法运行
- c++ - 在 C++ 中使用 `using` 公开私有基类名称
- c# - 除了使用 C# 使用 Multipart 表单数据之外,还有其他方式将文件发送到 AWS s3 存储桶吗?
- group-by - 如何使用 power bi DAX 使用 group by 和 summarise 函数获得相同的结果?