DocumentDB Primary Key Guidelines
I have a document class whose primary key is called PrimaryKey
public class Document<T> : BaseDocument, IDocument<T>
{
[JsonProperty(PropertyName = "id")]
public virtual T PrimaryKey { get; set; }
}
Since the primary key of the document is db id (lowercase required), you can replace id with JSONProperty from Newtonsoft.Json , which will automatically be serialized using the PrimaryKey value in the id name.
source to share
The most common practice I've seen is to just use the GUID assigned when creating a new document to be added / inserted. You can also specify an Indexing Policy . This can help support situations where you need to find a document based on other properties in those documents.
Creating a custom composite key is also useful if you usually know which document you want to retrieve. In this case, it is application-specific to determine the best way to create the composite value.
source to share
If you need incremental, numeric and unique keys, you can use the Javascript date new Date().getTime()
in milliseconds as a unique identifier for each document. The advantage of this is that if you need to search for documents by range, you can configure this property as a range index and do efficient range searches. Depending on your application, the chances of 2 documents being created in the same millisecond are pretty slim.
source to share