Create
Create( collection, param_object )
Description
The Create
function adds a new document to a collection. The
collection_ref
parameter indicates in what collection the document
should be created, while param_object
contains the document data and
optional metadata.
Parameters
Argument | Type | Definition and Requirements |
---|---|---|
|
The name, or reference, of the collection that should contain the new
document. A collection reference can be acquired using the
|
|
|
The |
param_object
Field Name | Field Type | Definition and Requirements | ||
---|---|---|---|---|
|
The user’s single, changeable document. |
|||
|
Optional - The permissions for this document. |
|||
|
||||
|
Optional - A timestamp indicating the document’s time-to-live, which is when the document should be removed. When a document is removed, the document’s existence ceases (as if it never existed); temporal queries cannot recover the document.
|
Returns
A document containing both the data and metadata about the results of the operations.
Field Name | Field Type | Definition and Requirements |
---|---|---|
|
The reference is an automatically generated identifier within the database uniquely identifying the document created. |
|
|
The data that was inserted with the document. |
|
|
The timestamp, with microsecond resolution, associated with the creation of the document. |
Examples
The following query creates a document by providing a reference to the
collection "spells" and a param_object
with a data
field. The data
field contains the user data to be inserted for this document.
client.query(
q.Create(
q.Collection('spells'),
{
data: {
name: 'Mountainous Thunder',
element: 'air',
cost: 15,
},
},
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
ts: 1527274715273882,
data: { name: 'Mountainous Thunder', element: 'air', cost: 15 } }
The following query creates a document in the posts
collection with a
specified id (via the Ref
function):
serverClient.query(
q.Create(
q.Ref(q.Collection('posts'), '1'),
{ data: { title: 'The first post' } },
)
)
.then((ret) => console.log(ret))
{
ref: Ref(Collection("posts"), "1"),
ts: 1587595447990000,
data: { title: 'The first post' }
}
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!