Documentation Home
X DevAPI User Guide for MySQL Shell in Python Mode
Download this Manual
PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb


4.3.1 Collection.add()

The Collection.add() function is for storing documents in a collection, similar to the INSERT statement for an SQL database. It takes a single document or a list of documents as its argument, and is executed by the execute() function.

The collection needs to be created with the Schema.createCollection() function before documents can be inserted. To insert documents into an existing collection use the Schema.getCollection() function to retrieve the Collection object.

The following example shows how to use the Collection.add() function. The example assumes that the test schema exists and that the collection my_collection does not exist.

# 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()

See also CollectionAddFunction for the syntax of add() in EBNF.