DocumentDB Primary Key Guidelines

I am developing a website with azure DocumentDB. I need to create a unique and short primary key.

Since there is no auto-incrementing in documentDB, is there a good way to deal with this issue.

I am awaiting a response with best practice recommendations. Thank you.

+3


source to share


3 answers


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.

+4


source


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.

+4


source


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.

+1


source







All Articles