AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

db.getCollection()(mongoshメソッド)

db.getCollection(name)

Returns a collection or a view object that is functionally equivalent to using the db.<collectionName> syntax. The method is useful for a collection or a view whose name might interact with mongosh itself, such as names that begin with _ or that match a database shell method.

The db.getCollection() method has the following parameter:

Parameter
タイプ
説明

name

string

コレクションの名前。

このメソッドは、次の環境でホストされている配置で使用できます。

  • 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 のバージョン

The db.getCollection() object can access any collection methods.

指定されたコレクションは、サーバー上に存在する場合と存在しない場合があります。コレクションが存在しない場合、MongoDB では書込み操作の一部として db.collection.insertOne() のようにコレクションを暗黙的に作成します。

The following example uses db.getCollection() to access the auth collection and insert a document into it.

var authColl = db.getCollection("auth")
authColl.insertOne(
{
usrName : "John Doe",
usrDept : "Sales",
usrTitle : "Executive Account Manager",
authLevel : 4,
authDept : [ "Sales", "Customers"]
}
)

以下の内容が返されます。

{
"acknowledged" : true,
"insertedId" : ObjectId("569525e144fe66d60b772763")
}

The previous example requires the use of db.getCollection("auth") because of a name conflict with the database method db.auth(). Calling db.auth directly to perform an insert operation would reference the db.auth() method and would error.

The following example attempts the same operation, but without using the db.getCollection() method:

db.auth.insertOne(
{
usrName : "John Doe",
usrDept : "Sales",
usrTitle : "Executive Account Manager",
authLevel : 4,
authDept : [ "Sales", "Customers"]
}
)

db.auth() メソッドに insertOne メソッドがないため、操作エラーが発生します。

このページを評価