为了创建一个新的集合,
createCollection()
从 Schema 对象调用该函数。它返回一个可以立即使用的 Collection 对象,例如,将文档插入数据库。
或者,对于连接器,该字段
reuseExistingObject
可以设置为 true 并作为第二个参数传递,以防止在已存在同名集合时生成错误。
MySQL 外壳 JavaScript 代码
// Create a new collection called 'my_collection'
var myColl = db.createCollection('my_collection');
MySQL 外壳 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);