定義
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
例
You can use db.getSiblingDB() as an alternative to the use <database> helper. This is particularly useful when writing scripts using mongosh where the use helper is not available.
2 つのデータベース users と records を持つ MongoDB インスタンスを考えてみましょう。active コレクションは users データベースの一部です。requests コレクションは records データベースの一部です。
データベースの指定
この操作は、db オブジェクトを users という名前のデータベースにポイントし、active コレクションの document count を返します。
db = db.getSiblingDB('users') db.active.countDocuments()
複数のデータベースの使用
次の一連の操作のように、異なるデータベースを参照する複数の db オブジェクトを作成できます。
users = db.getSiblingDB('users') records = db.getSiblingDB('records') users.active.countDocuments() users.active.findOne() records.requests.countDocuments() records.requests.findOne()
この操作により、2 つの db オブジェクトが作成されます。各 db オブジェクトは、異なるデータベース、users または records を参照します。
データベースごとに、クエリは以下を返します。
document count、および
そのデータベース内のコレクションから。