为了检索存储在数据库中的现有集合的 Collection 对象,请
        getCollection()从 Schema 对象调用该函数。
      
        如果该集合在数据库中尚不存在,则任何后续调用 Collection 对象函数都会抛出错误;对于某些连接器,您可以通过将
        validateExistence字段设置为 true 并将其作为第二个参数传递给db.getCollection().
      
MySQL 外壳 JavaScript 代码
// Get a collection object for 'my_collection'
var myColl = db.getCollection('my_collection');MySQL 外壳 Python 代码
# Get a collection object for 'my_collection'
myColl = db.get_collection('my_collection')Node.js JavaScript 代码
// Get a collection object for 'my_collection'
var collection = db.getCollection('my_collection');C#代码
// Get a collection object for "my_collection"
var myColl = db.GetCollection("my_collection");
// Get a collection object but also ensure it exists in the database
var myColl2 = db.GetCollection("my_collection", ValidateExistence: true);Python代码
# Get a collection object for 'my_collection'
my_coll = my_schema.get_collection('my_collection')
# Get a collection object but also ensure it exists in the database
my_coll = my_schema.get_collection('my_collection', true)Java代码
// Get a collection object for 'my_collection'
Collection myColl = db.getCollection("my_collection");
// Get a collection object but also ensure it exists in the database
// Second parameter is: boolean requireExists
Collection myColl = db.getCollection("my_collection", true);C++代码
// Get a collection object for 'my_collection'
Collection myColl = db.getCollection("my_collection");
// Get a collection object but also ensure it exists in the database
Collection myColl = db.getCollection("my_collection", true);
        与设置为 true的字段createCollection()一起
        ReuseExistingObject,可用于创建新集合或重用具有给定名称的现有集合。有关详细信息,请参见第 4.2.1 节 “创建集合” 
        。
      
笔记
在大多数情况下,良好的做法是在开发期间创建数据库对象,并避免在数据库项目的生产阶段动态创建它们。因此,最好将在数据库中创建集合的代码与实际的用户应用程序代码分开。