FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

Fauna accounts created after August 21, 2024 must use FQL v10. These accounts will not be able to run FQL v4 queries or access the v4 Dashboard.

For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions.

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:

client.query(
  q.Map(
    [
      { material: 'straw' },
      { material: 'sticks' },
      { material: 'bricks' },
    ],
    q.Lambda(
      'data',
      q.Create(
        q.Collection('dilapidated_huts'),
        { data: q.Var('data') },
      )
    )
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[
  {
    ref: Ref(Collection("dilapidated_huts"), "329482154362798144"),
    ts: 1650477518490000,
    data: {
      material: "straw"
    }
  },
  {
    ref: Ref(Collection("dilapidated_huts"), "329482154363846720"),
    ts: 1650477518490000,
    data: {
      material: "sticks"
    }
  },
  {
    ref: Ref(Collection("dilapidated_huts"), "329482154363847744"),
    ts: 1650477518490000,
    data: {
      material: "bricks"
    }
  }
]
result = client.query(
  q.map_(
    q.lambda_(
      "data",
      q.create(
        q.collection("dilapidated_huts"),
        { "data": q.var("data") },
      ),
    ),
    [
      { "material": "straw" },
      { "material": "sticks" },
      { "material": "bricks" }
    ]
  )
)
print(result)
[{'ref': Ref(id=329483608445157953, collection=Ref(id=dilapidated_huts, collection=Ref(id=collections))), 'ts': 1650477518490000, 'data': {'material': 'straw'}}, {'ref': Ref(id=329483608446206529, collection=Ref(id=dilapidated_huts, collection=Ref(id=collections))), 'ts': 1650477518490000, 'data': {'material': 'sticks'}}, {'ref': Ref(id=329483608446207553, collection=Ref(id=dilapidated_huts, collection=Ref(id=collections))), 'ts': 1650477518490000, 'data': {'material': 'bricks'}}]
result, err := client.Query(
	f.Map(
		f.Arr{
			f.Obj{"material": "straw"},
			f.Obj{"material": "sticks"},
			f.Obj{"material": "bricks"},
		},
		f.Lambda(
			"data",
			f.Create(
				f.Collection("dilapidated_huts"),
				f.Obj{"data": f.Var("data")},
			),
		),
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[map[data:map[material:straw] ref:{329482154362798144 0xc000180570 0xc000180570 <nil>} ts:1650477518490000], map[data:map[material:sticks] ref:{329482154363846720 0xc000180570 0xc000180570 <nil>} ts:1650477518490000], map[data:map[material:bricks] ref:{329482154363847744 0xc000180570 0xc000180570 <nil>} ts:1650477518490000]]
try
{
    Value result = await client.Query(
        Map(
            Arr(
                Obj("material", "straw"),
                Obj("material", "sticks"),
                Obj("material", "bricks")
            ),
            Lambda(
                "data",
                Create(
                    Collection("dilapidated_huts"),
                    Obj("data", Var("data"))
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(ObjectV(ref: RefV(id = "329483608445157953", collection = RefV(id = "dilapidated_huts", collection = RefV(id = "collections"))),ts: LongV(1650477518490000),data: ObjectV(material: StringV(straw))), ObjectV(ref: RefV(id = "329483608446206529", collection = RefV(id = "dilapidated_huts", collection = RefV(id = "collections"))),ts: LongV(1650477518490000),data: ObjectV(material: StringV(sticks))), ObjectV(ref: RefV(id = "329483608446207553", collection = RefV(id = "dilapidated_huts", collection = RefV(id = "collections"))),ts: LongV(1650477518490000),data: ObjectV(material: StringV(bricks))))
System.out.println(
    client.query(
        Map(
            Arr(
                Obj("material", Value("straw")),
                Obj("material", Value("sticks")),
                Obj("material", Value("bricks"))
            ),
            Lambda(
                Value("data"),
                Create(
                    Collection("dilapidated_huts"),
                    Obj("data", Var("data"))
                )
            )
        )
    ).get());
[{ref: ref(id = "329483608445157953", collection = ref(id = "dilapidated_huts", collection = ref(id = "collections"))), ts: 1650477518490000, data: {material: "straw"}}, {ref: ref(id = "329483608446206529", collection = ref(id = "dilapidated_huts", collection = ref(id = "collections"))), ts: 1650477518490000, data: {material: "sticks"}}, {ref: ref(id = "329483608446207553", collection = ref(id = "dilapidated_huts", collection = ref(id = "collections"))), ts: 1622573879370000, data: {material: "bricks"}}]
Map(
  [
    { material: 'straw' },
    { material: 'sticks' },
    { material: 'bricks' }
  ],
  Lambda(
    "data",
    Create(
      Collection("dilapidated_huts"),
      { data: Var("data") }
    )
  )
)
[
  {
    ref: Ref(Collection("dilapidated_huts"), "329482154362798144"),
    ts: 1650477518490000,
    data: {
      material: "straw"
    }
  },
  {
    ref: Ref(Collection("dilapidated_huts"), "329482154363846720"),
    ts: 1650477518490000,
    data: {
      material: "sticks"
    }
  },
  {
    ref: Ref(Collection("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!