Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

db.getSiblingDB()

項目一覧

  • 定義
db.getSiblingDB(<database>)
Parameter
タイプ
説明
database
string
MongoDB データベースの名前。
次の値を返します。データベース オブジェクト。

shell 環境の db 変数を変更せずに別のデータベースを返すために使用されます。

use <database>ヘルパーの代替として db.getSiblingDB()を使用できます。 これは、 useヘルパーが使用できないmongoshを使用してスクリプトを作成する場合に特に便利です。

2 つのデータベース usersrecords を持つ 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、および

  • an example document

そのデータベース内のコレクションから。

戻る

db.getReplicationInfo

項目一覧