【发布时间】:2021-12-14 14:32:01
【问题描述】:
我正在像这样连接到 mongo db:
$mongoClient = new MongoDB\Client($db_string);
像这样获取集合:
$collection = $mongoClient->selectCollection('database_name', 'collection_name');
并像这样获取集合迭代器:
$iterator = $collection->find();
但是最后一次调用会出错:
[错误] 无法从 demo_article 检索源计数:身份验证失败。
我在这里做错了什么?
更新:
这里:
protected function initializeIterator()
{
$this->iterator = $this->collection->find();
if($this->iterator instanceof Traversable) {
echo "**Traversable!**";
}
迭代器是可遍历的。但是,这段代码是从 SourcePluginBase 中调用的:
protected function doCount() {
$iterator = $this->getIterator();
if($iterator instanceof Traversable) {
echo "**TRAVERSABLE!**";
}else{
echo "**NOT TRAVERSABLE!**";
}
它不是可遍历的?!它怎么会失去可遍历的状态?
【问题讨论】:
-
并不是说你个人做错了,但这里出了问题 - 根据错误消息 - 是身份验证。您是否考虑过这可能是一个问题?如果是/否,为什么或为什么不?
-
@hakre 我正在编写迁移,因此我的源类需要迭代器。所以想法是连接到数据库,选择集合然后获取它的迭代器。如果是关于身份验证,那么在获取迭代器而不是在第一步时,连接到数据库时会发生什么?