PDF(US Ltr) - 1.2Mb
PDF(A4) - 1.2Mb
Collection.add() 函数用于将文档存储在集合中,类似于 SQL 数据库中 INSERT 语句。它接受单个文档或文档列表作为参数,并由 execute() 函数执行。
在可以插入文档之前,需要使用 Schema.createCollection() 函数创建集合。要将文档插入现有集合,请使用 Schema.getCollection() 函数检索 Collection 对象。
以下示例展示了如何使用 Collection.add() 函数。该示例假设 test 模式存在,并且 my_collection 集合不存在。
# Create a new collection
myColl = db.create_collection('my_collection')
# Insert a document
myColl.add({ 'name': 'Laurie', 'age': 19 }).execute()
# Insert several documents at once
myColl.add([
{ 'name': 'Nadya', 'age': 54 },
{ 'name': 'Lukas', 'age': 32 } ]).execute()
另请参见 CollectionAddFunction 以了解 EBNF 中 add() 的语法。