独立选择DB和collection

@param string $dbName MongoDB Name
@param string $collectionName MongoDB Collection Name
@return \MongoCollection
185555
public function setDbCollection(string $dbName, string $collectionName): \MongoCollection
    {
        return $this->mongoClient->selectDB($dbName)->selectCollection($collectionName);
    }
199937
public static MongoCollection<Document> createCollection(MongoDatabase db,
            String collectionName, CreateCollectionOptions options) {
        db.createCollection(collectionName, options);
        return db.getCollection(collectionName);
    }
634964
public function getCollection($db = null)
    {
        if ($db === null) {
            $db = Yii::$app->get('mongodb');
        }

        return $db->getCollection($this->from);
    }
185556
protected function fetchDBRef($typeName, $dbref)
    {
        $typeName = $this->getFullType($typeName);
        if (class_exists($typeName) && MongoDBRef::isRef($dbref)) {
            $collectionName = $typeName::getCollection();
            $collection = $this->_collection->db->$collectionName;
            $data = $this->_cache->fetch("{$collectionName}_{$dbref['$id']}");
            if ($data === null) {
                $data = $this->_collection->getDBRef($dbref);
            }
            if ($data === null) {
                return null;
            }
            return new $typeName($data, $collection, $this->_cache);
        }
        return null;
    }
685407
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $config = $this->getConfig($serviceLocator)[$requestedName];
        /** @var $mongodb \MongoDb */
        $mongodb = $serviceLocator->get($config['database']);
        return $mongodb->selectCollection($config['collection']);
    }
1162450
public function getCollection()
    {
        $collection = trim(str_replace('\\', '_', $this->collection), '_');
        return $this->$collection()
            ->setDatabase($this->database)
            ->setTable($this->table)
            ->setModel($this->model)
            ->set($this->getRows());
    }
1054004
public function getTables($dbName = null)
	{
		$sql = <<<SQL
SELECT
	name
FROM
	(
		SELECT
			*
		FROM
			sqlite_master
		UNION ALL
			SELECT
				*
			FROM
				sqlite_temp_master
	)
WHERE
	type IN ('table', 'view')
SQL;
		// 查询
		$result = $this->query($sql);
		if (false === $result)
		{
			// 失败
			$r = false;
			return $r;
		}
		else
		{
			$keys = array_keys($result[0]);
			$r = array ();
			// 处理数据
			foreach ($result as $value)
			{
				$r[] = $value[$keys[0]];
			}
			// 返回结果
			return $r;
		}
	}
429569
public static function showDbTables($dbname, $where = null)
    {
        $query = static::getQuery();

        return static::build(
            'SHOW',
            'TABLE STATUS FROM',
            $query->quoteName($dbname),
            $where ? new QueryElement('WHERE', $where, 'AND') : null
        );
    }
512043
public static boolean collectionExists(MongoDatabase db, String collectionName) {
        return db.listCollections().filter(Filters.eq("name", collectionName)).first() != null;
    }
1132642
public function find($collectionName, array $criteria = [], array $fields = []) {
		$collection = $this->getCollection($collectionName);
		
		if ($this->_profilerEnabled) {
			$message = 'db.' . $collectionName . '.find(';

			if (empty($criteria)) {
				$message .= ');';
			} else {
				$message .= json_encode($criteria) . ');';
			}

			$event = $this->getProfiler()->startRead($message);
		}

		$cursor = $collection->find($criteria, $fields);

		if ($this->_profilerEnabled) {
			$event->stopSuccess();
		}

		return $cursor;
	}
859442
public void createCollection(final MongoOperations mongoTemplate, final String collectionName, final boolean dropCollection) {
        if (dropCollection) {
            LOGGER.trace("Dropping database collection: [{}]", collectionName);
            mongoTemplate.dropCollection(collectionName);
        }

        if (!mongoTemplate.collectionExists(collectionName)) {
            LOGGER.trace("Creating database collection: [{}]", collectionName);
            mongoTemplate.createCollection(collectionName);
        }
    }
759337
public function collection($collectionId)
    {
        return new CollectionReference(
            $this->connection,
            $this->valueMapper,
            $this->childPath($this->name, $collectionId)
        );
    }
821049
public function getMedia(string $collectionName = 'default', $filters = []): Collection
    {
        return $this->getMediaRepository()->getFilteredCollection($collectionName, $filters);
    }
139263
public function addSchemaCollection($collectionName, $schemas)
    {
        $this->schemaCollections[$collectionName] = [];

        foreach ($schemas as $schemaName => $schema) {
            $this->addSchema($collectionName, $schemaName, $schema);
        }
    }
19315
private static function collection(string $name): \MongoCollection
    {
        // 记录开始时间
        $start = timeLog();

        // 取此集合的配置
        $config = configDefault(null, 'mongo', $name);
        if (!$config) {
            trigger_error('读取Mongo服务器配置失败:' . $name, E_USER_ERROR);
        }

        // 智能识别配置信息
        if (is_array($config)) {
            $server = $config['server'];
            if (isset($config['collection'])) {
                $collection = $config['collection'];
            } else {
                $collection = $name;
            }
        } else {
            $server = $config;
            $collection = $name;
        }

        // 连接指定数据库
        $db = self::client($server);

        // 分解库名
        $arr = explode('/', $server);
        $dbname = end($arr);

        // 记录调试信息
        debug("connect to mongo $name ,persist:" . timeLog($start) . 'ms');

        // 返回集合对象
        try {
            return $db->selectCollection($dbname, $collection);
        } catch (\Exception $e) {
            trigger_error('Mongo集合名称错误:' . $name, E_USER_ERROR);
        }

    }
1282126
public function selectCollection($db_name, $collection_name)
    {
        $db = new Db((String) $db_name, $this);
        $collection = $db->selectCollection($collection_name);

        return $collection;
    }
377637
private MongoCollection<Document> getDBCollection(String docName) {
        if (docName == null || docName.length() == 0) {
            return null;
        }
        return mongoDatabase.getCollection(docName);
    }