/** * List collections in a DB * * @param MongoDB $db DB * @return array<MongoCollection> */ static function listCollections(MongoDB $db) { $server = MServer::currentServer(); $names = array(); $query = $db->execute("function (){ return db.getCollectionNames(); }", array()); if ($query["ok"]) { $names = $query["retval"]; } else { $colls = $db->listCollections(true); foreach ($colls as $coll) { $names[] = $coll->getName(); } } $ret = array(); foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { continue; } $ret[] = $name; } sort($ret); //system collections if (!$server->uiHideSystemCollections()) { foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { $ret[] = $name; } } } $collections = array(); foreach ($ret as $v) { if ($v === "") { //older MongoDB version (maybe before 1.7) allow empty collection name continue; } $collections[] = $db->selectCollection($v); } return $collections; }
/** * List collections in a DB * * @param MongoDB $db DB * @return array<MongoCollection> */ static function listCollections(MongoDB $db) { $server = MServer::currentServer(); $names = array(); try { $names = $db->getCollectionNames(true); } catch (Exception $e) { } $ret = array(); foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { continue; } $ret[] = $name; } sort($ret); //system collections if (!$server->uiHideSystemCollections()) { foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { $ret[] = $name; } } } $collections = array(); foreach ($ret as $v) { if ($v === "") { //older MongoDB version (maybe before 1.7) allow empty collection name continue; } $collections[] = $db->selectCollection($v); } return $collections; }