Create a collection

Problem

You need to create a new collection in the current database to store documents.

Solution

Use the CreateCollection function:

try
{
    Value result = await client.Query(
        CreateCollection(Obj("name", "huts"))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "huts", collection = RefV(id = "collections")),ts: LongV(1622573782400000),history_days: LongV(30),name: StringV(huts))
result, err := client.Query(
	f.CreateCollection(f.Obj{"name": "huts"}))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[history_days:30 name:huts ref:{huts 0xc0001837a0 0xc0001837a0 <nil>} ts:1622573814740000]
System.out.println(
    client.query(
        CreateCollection(Obj("name", Value("huts")))
    ).get());
{ref: ref(id = "huts", collection = ref(id = "collections")), ts: 1622573835420000, history_days: 30, name: "huts"}
client.query(
  q.CreateCollection({ name: 'huts' })
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Collection("huts"),
  ts: 1622573894320000,
  history_days: 30,
  name: 'huts'
}
result = client.query(
  q.create_collection({"name": "huts"})
)
print(result)
{'ref': Ref(id=huts, collection=Ref(id=collections)), 'ts': 1622573899290000, 'history_days': 30, 'name': 'huts'}
try {
  println(Await.result(
    client.query(
        CreateCollection(Obj("name" -> "huts"))
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "huts", collection = ref(id = "collections")), ts: 1622573935450000, history_days: 30, name: "huts"}
CreateCollection({ name: 'huts' })
{
  ref: Collection("huts"),
  ts: 1624310571870000,
  history_days: 30,
  name: 'huts'
}

More information

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!