【发布时间】:2019-09-16 20:38:46
【问题描述】:
之前我通过查询命名空间来检查集合是否存在。
大致这样,检查“foo.bar”是否存在:
return 1 === $client->selectCollection('foo','system.namespaces');
->count(['name'=>'bar']);
由于这仅适用于 mmapv1 并且我已移至 wiredTiger,因此我尝试了此操作,而不是依靠驱动程序抛出“数据库 foo 不存在”或“集合栏不存在”。
try {
$command = new MongoDB\Driver\Command(['listIndexes'=>'bar']);
$server->executeReadCommand('foo',$command);
return true;
}
catch( MongoDB\Driver\Exception\CommandException $e ){
return false;
}
我不想列出集合,因为集合有数千个,但我不喜欢依赖异常,因为我注意到从 3.6 迁移到 4.0 时错误消息发生了变化。
什么是存储引擎不可知论和未来版本证明的正确方法?
【问题讨论】:
标签: mongodb php-mongodb