Inserting a new document with the document ID as the key


Rather than using the UUID() function to generate a document key, I was wondering if it’s possible to use the document ID as the key?


Yes, you can specify the document ID yourself instead of using UUID.


How do you specify the document ID? I don’t mean the key (a string), but the document ID (a 64-bit number AFAIK).


Sorry, I don’t think I was very clear with my initial wording of my question.

I was just looking to have a UUID that’s a bit shorter than the strings produced by UUID(). Since the document IDs are unique for a bucket, then I would have thought that in theory that could be used as the key (converted to base-10 or base-16 for example) when inserting the document. However AFAIK you can’t know the document ID until after you’ve inserted the document.

How are you inserting the docs? If you’re using N1QL then just apply a function over UUID to trim/convert it

SUBSTR(UUID(), 0, 10),
{ “id”: “01”, “type”: “airline”}
RETURNING META().id as docid, *;

1 Like

Yes I’m using N1QL.

I’m currently using UUID() for the key (removing the hyphens only), but was wondering if it would be possible to use the value that is returned by META().id instead.

Unless I’m mistaken, just trimming the UUID() returned value won’t do that, and presumably there would be the possible chance of duplication if it were trimmed.

Using UUID() is fine as a key. It was more of a curiosity to know if it’s possible to use the META().id value instead.


The META().id is the document ID that was set.

When you create a document with the ID created by UUID(), whatever random value was created would then be the META().id for that document.

1 Like


For some reason I was thinking that the ID was a 64-bit integer, and don’t have access to a CB installation right now to check.

Thanks for clearing that up for me!