Create a document in a collection

Problem

You need to create a document in an existing collection in the current database.

Solution

Use the Create function:

Copied!
client.query(
  q.(
    q.('dilapidated_huts'),
    { data: { material: 'straw' } }
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: (("dilapidated_huts"), "300223085354680832"),
  ts: 1622573895720000,
  data: { material: 'straw' }
}
Query metrics:
  •    bytesIn:  105

  •   bytesOut:  198

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:    0

  • writeBytes:  208

  •  queryTime: 37ms

  •    retries:    0

Discussion

The Create function needs both the Reference to a collection and the document’s data.

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!