CreateCollection

CreateCollection( param_object )
CreateCollection( param_object )
CreateCollection( param_object )
CreateCollection( param_object )
create_collection( param_object )
create_collection name
CreateCollection( param_object )
CreateCollection( param_object )

Description

The CreateCollection function is used to create a collection that groups documents. Once the collection has been created, it is possible to create documents within the collection. You cannot create a collection and insert documents into that collection in the same transaction.

Unique name required

The collection name must be unique within the database. If you try to lookup a collection by name and only create it if it does not exist, the operation might fail if another transaction has created the collection in the meantime.

Parameters

Argument Type Definition and Requirements

param_object

Object

The param_object fields are described below.

param_object

Field Name Field Type Definition and Requirements

name

String

The name of the collection. Collections cannot be named any of the following reserved words: events, set, self, documents, or _.

data

Object

Optional - This is user-defined metadata for the collection. It is provided for the developer to store information at the collection level.

history_days

Long

Optional - The number of days that document history is retained for in this collection. Setting history_days to null retains this collection’s history forever. It defaults to 30 days.

ttl_days

Long

Optional - The number of days documents are retained for this collection. Documents which have not been updated within the configured TTL duration are removed. Setting ttl_days to null retains documents forever. The default is null.

permissions

Object

Optional - Provides the ability to enable permissions at the collection level. See collection permissions for additional details.

Returns

An object containing the fields returned by the CreateCollection function is described below.

Field Name Field Type Definition and Requirements

ref

Reference

The reference is an automatically generated identifier within the database to uniquely identify the collection that was created.

name

String

The name of the collection that was created.

ts

Long

The timestamp associated with the creation of the document.

history_days

Long

The number of days to retain history.

Examples

The query below creates a collection called "boons" with defaults.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "create_collection": { "object": { "name": "boons" } } }'
client.Query(CreateCollection(Obj("name", "boons")));
System.out.println(
      client.query(
            CreateCollection(Obj("name", Value("boons")))
      ).get());
result, _ := client.Query(f.CreateCollection(f.Obj{"name": "boons"}))

fmt.Println(result)
client.query(CreateCollection(Obj("name" -> "boons")))
client.query(q.create_collection({"name": "boons"}))
$client.query do
  create_collection name: 'boons'
end
client.query(CreateCollection(Obj("name" => "boons")))
client.query(q.CreateCollection({ name: "boons" }))
  .then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
  "resource": {
    "ref": { "@ref": "classes/boons" },
    "collection": { "@ref": "collections" },
    "ts": 1509244539971619,
    "history_days": 30,
    "name": "boons"
  }
}
{
  "ref": { "@ref": "classes/boons" },
  "collection": { "@ref": "collections" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
{
  ref: ref(id = "boons", collection = ref(id = "collections")),
  ts: 1526674566802938,
  history_days: 30,
  name: "boons"
}
map[ref:{boons 0xc4201f2fa0 <nil>} ts:1527277025406385 history_days:30 name:boons]
{
  "ref": { "@ref": "classes/boons" },
  "collection": { "@ref": "collections" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
{
  "ref": { "@ref": "classes/boons" },
  "collection": { "@ref": "collections" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
{
  "ref": { "@ref": "classes/boons" },
  "collection": { "@ref": "collections" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
{
  "ref": { "@ref": "classes/boons" },
  "collection": { "@ref": "collections" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
{ ref: Ref(id=boons, collection=Ref(id=collections)),
  ts: 1527274777496292,
  history_days: 30,
  name: 'boons' }

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!