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

For more details, review the migration guide. Contact support@fauna.com with any questions.

Collection metadata

Problem

You need to store additional metadata in a collection document.

Solution

Set the data field in a collection:

adminClient.query(
  q.Update(
    q.Collection('dilapidated_huts'),
    { data: { env: 'test' } }
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: Collection("dilapidated_huts"),
  ts: 1635183086600000,
  history_days: 30,
  name: 'dilapidated_huts',
  data: { env: 'test' }
}
result = adminClient.query(
  q.update(
    q.collection("dilapidated_huts"),
    {"data": {"env": "test"}}
  )
)
print(result)
{'ref': Ref(id=dilapidated_huts, collection=Ref(id=collections)), 'ts': 1635183090850000, 'history_days': 30, 'name': 'dilapidated_huts', 'data': {'env': 'test'}}
result, err := adminClient.Query(
	f.Update(
		f.Collection("dilapidated_huts"),
		f.Obj{"data": f.Obj{"env": "test"}},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:map[env:test] history_days:30 name:dilapidated_huts ref:{dilapidated_huts 0xc0001804b0 0xc0001804b0 <nil>} ts:1635183036860000]
try
{
    Value result = await client.Query(
        Update(
            Collection("dilapidated_huts"),
            Obj("data", Obj("env", "test"))
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "dilapidated_huts", collection = RefV(id = "collections")),ts: LongV(1635183024850000),history_days: LongV(30),name: StringV(dilapidated_huts),data: ObjectV(env: StringV(test)))
Update(
  Collection('dilapidated_huts'),
  { data: { env: 'test' } }
)
{
  ref: Collection("dilapidated_huts"),
  ts: 1635183253930000,
  history_days: 30,
  name: 'dilapidated_huts',
  data: { env: 'test' }
}
Query metrics:
  •    bytesIn:   99

  •   bytesOut:  186

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:  227

  • writeBytes:  113

  •  queryTime: 18ms

  •    retries:    0

Discussion

Adding metadata to a collection can be used to store any information that helps to distinguish the collection from other collections.

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!