On this page本页内容
db.
getCollection
(name)¶Returns a collection or a view object that is functionally equivalent to using the 返回功能等同于使用db.<collectionName>
syntax. db.<collectionName>
语法的集合对象或视图对象。The method is useful for a collection or a view whose name might interact with the 该方法对于名称可能与mongo
shell itself, such as names that begin with _
or that match a database shell method.mongo
shell本身交互的集合或视图非常有用,例如以_
开头的名称或与数据库shell方法匹配的名称。
The db.getCollection()
method has the following parameter:db.getCollection()
方法具有以下参数:
name |
string |
The db.getCollection()
object can access any collection methods.db.getCollection()
对象可以访问任何集合方法。
The collection specified may or may not exist on the server. 服务器上可能存在或不存在指定的集合。If the collection does not exist, MongoDB creates it implicitly as part of write operations like 如果集合不存在,MongoDB将隐式创建它,作为诸如db.collection.insertOne()
.db.collection.insertOne()
之类的写操作的一部分。
The following example uses 下面的示例使用db.getCollection()
to access the auth
collection and insert a document into it.db.getCollection()
访问auth
集合并将文档插入其中。
This returns:它返回:
The previous example requires the use of 上一个示例要求使用db.getCollection("auth")
because of a name conflict with the database method db.auth()
. db.getCollection("auth")
,因为它与数据库方法db.auth()
存在名称冲突。Calling 直接调用db.auth
directly to perform an insert operation would reference the db.auth()
method and would error.db.auth
来执行插入操作将引用db.auth()
方法,并且会出错。
The following example attempts the same operation, but without using the 以下示例尝试相同的操作,但未使用db.getCollection()
method:db.getCollection()
方法:
The operation errors as 操作错误为db.auth()
method has no insertOne
method.db.auth()
方法没有insertOne
方法。
See also参阅