NewId

NewId()
NewId()
NewId()
NewId()
new_id()
NewId()

Description

The NewId function produces a number which is guaranteed to be unique across all databases and is suitable for constructing the document ID part of a Reference.

Fauna’s document IDs are generated using the Twitter Snowflake algorithm. The IDs are based on time instead of being sequential, and are generally increasing. As such, the NewId function should not be used to generate random numbers.

FQL does not provide random number generation, as its operations attempt to be as deterministic as possible.

If you need a source of random numbers, your host language or browser can provide high-performance random number generation. Once you have generated a random number, you can then use it within your queries in deterministic fashion.

Parameters

None.

Returns

A unique numeric String.

Examples

The following query returns a unique string-encoded 64-bit integer:

try
{
    Value result = await client.Query(
        NewId()
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
StringV(280491725429932544)
result, err := client.Query(
	f.NewId())

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
280481920441123328
System.out.println(
    client.query(
        NewId()
    ).get());
"181388643266462208"
client.query(
  q.NewId()
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
181388643266462208
result = client.query(
  q.new_id()
)
print(result)
"181388643266462208"
NewId()
'302044032378339840'
Query metrics:
  •    bytesIn:  15

  •   bytesOut:  33

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 2ms

  •    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!