来自 Shiju Varghese 的《使用 Go 进行 Web 开发》
To perform CRUD operations into MongoDB, an object of
*mgo.Collection
is created, which represents the MongoDB collection. You can create an object of*mgo.Collection
by calling methodC
of*mgo.Database
.The
mgo.Database
type represents the named database that can be created by calling theDB
method of*mgo.Session
.Listing 8-5 accesses the MongoDB collection named "categories".
Listing 8-5. Accessing a MongoDB Collection
c := session.DB("taskdb").C("categories")
DB
和 C
方法在 Go 程序中是否都创建数据库对象和集合对象
用于 MongoDB 服务器中的现有数据库和现有集合?
对于不存在的数据库和不存在的集合,以便这些方法将在 MongoDB 服务器中创建新的数据库和集合,并将其命名为方法的参数?
谢谢。
最佳答案
在 MongoDB 中,创建一个新的数据库和集合仅在写入操作之后,在特定数据库中插入第一个文档之后并且收藏。
在 MongoDB 中没有像命令/函数那样的 CREATE TABLE
,不需要那个。
您的代码不会创建 集合,因为不会在新集合中插入新文档。在您的代码中,您无需担心创建数据库或集合
关于mongodb - 这些方法是否在 MongoDB 服务器中创建新的数据库和集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730677/