PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb
为了创建一个新集合,请从 Schema 对象调用 createCollection()
函数。它会返回一个 Collection 对象,您可以立即使用它来执行操作,例如将文档插入到数据库中。
可选地,对于连接器,字段 reuseExistingObject
可以设置为 true 并作为第二个参数传递,以防止在同名集合已存在时产生错误。
MySQL Shell JavaScript 代码
// Create a new collection called 'my_collection'
var myColl = db.createCollection('my_collection');
MySQL Shell Python 代码
# Create a new collection called 'my_collection'
myColl = db.create_collection('my_collection')
Node.js JavaScript 代码
// Create a new collection called 'my_collection'
var promise = db.createCollection('my_collection');
// Create a new collection or reuse existing one
var promise = db.createCollection('my_collection', { ReuseExistingObject: true } );
C# 代码
// Create a new collection called "my_collection"
var myColl = db.CreateCollection("my_collection");
// Create a new collection or reuse existing one
var myExistingColl = db.CreateCollection("my_collection", ReuseExistingObject: true);
Python 代码
# Create a new collection called 'my_collection'
my_coll = my_schema.create_collection('my_collection')
# Create a new collection or reuse existing one
my_coll = my_schema.create_collection('my_collection', True)
Java 代码
// Create a new collection called 'my_collection'
Collection myColl = db.createCollection("my_collection");
// Create a new collection or reuse existing one
// Second parameter is: boolean reuseExistingObject
Collection myExistingColl = db.createCollection("my_collection", true);
C++ 代码
// Create a new collection called 'my_collection'
Collection myColl = db.createCollection("my_collection");
// Create a new collection or reuse existing one
Collection myExistingColl = db.createCollection("my_collection", true);