Collection.create()
Learn: Collection schema |
---|
We recommend you use FSL to create and update user-defined collections. See FSL collection schema. |
Create a collection.
Description
The Collection.create()
method creates a collection definition
document with the provided data, which must include a unique
collection name.
Collection.create({ name: "Inventory" })
You can’t create a collection and use it in the same query. Use separate queries instead.
Considerations
If a database has staged schema, this method adds a collection to the staged schema, not the active schema.
If the database has no staged schema, using this method is equivalent to making an unstaged schema change. Changes are applied immediately to the database’s active schema.
Unstaged schema changes that trigger an index build may result in downtime where the index is not queryable.
Avoid concurrent schema changes
Concurrent unstaged schema changes can cause contended transactions, even if the changes affect different resources. This includes unstaged changes made using:
A schema change triggers a transaction that validates the entire database schema. To avoid errors, do one of the following instead:
-
Perform unstaged schema changes sequentially
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
data |
Yes |
Object describing the collection. |
data fields
See
Collection
documents.
Return value
Type | Description |
---|---|
Collection definitions, represented as a
|
Examples
Create a collection named Inventory
without defining fields for the
collection:
Collection.create({ name: "Inventory" })
{
name: "Inventory",
coll: Collection,
ts: Time("2099-02-18T20:49:36.680Z"),
history_days: 0,
indexes: {},
constraints: []
}
Fields can be added and changed with the
document.update()
and
document.replace()
methods.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!