Create many documents in a collection

Problem

You need to create many documents in a collection within the current database.

Solution

Use the Map function to iterate over an array, and use the Create function to create a new document for each item in the array:

Copied!
(
  [
    { material: 'straw' },
    { material: 'sticks' },
    { material: 'bricks' }
  ],
  (
    "data",
    (
      ("dilapidated_huts"),
      { data: ("data") }
    )
  )
)
[
  {
    ref: (("dilapidated_huts"), "329482154362798144"),
    ts: 1650477518490000,
    data: {
      material: "straw"
    }
  },
  {
    ref: (("dilapidated_huts"), "329482154363846720"),
    ts: 1650477518490000,
    data: {
      material: "sticks"
    }
  },
  {
    ref: (("dilapidated_huts"), "329482154363847744"),
    ts: 1650477518490000,
    data: {
      material: "bricks"
    }
  }
]
Query metrics:
  •    bytesIn:   168

  •   bytesOut:   458

  • computeOps:     2

  •    readOps:     0

  •   writeOps:     3

  •  readBytes: 1,223

  • writeBytes: 2,289

  •  queryTime:  36ms

  •    retries:     0

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!