How to create a auto incremented column in Documentdb

satish kumar V picture satish kumar V · Dec 30, 2014 · Viewed 11k times · Source

I want to create a document in azure documentdb with an auto-increment column.

Is this possible? If yes, please guide me.

Any help would be greatly appreciated.

Database db = CreateOrReadDocumentDb("EmployeeDb").Result;
DocumentCollection dc = CreateOrReadDocumentCollection(db.SelfLink, "EmployeeDetails").Result;
Employee emp = new Employee();
emp.Name="ABC";
emp.id="";//automatically generate a unique string
emp.sal=10000;
emp.exp =5;
emp.index=0; // I want an auto increment column for emp with name index and want to store in azure document db
 client.CreateDocumentAsync(collectionLink, data);

Answer

Andrew Liu picture Andrew Liu · Jan 5, 2015

DocumentDB doesn't include auto-increment functionality out of the box.

As Gaurav mentioned in Do we have Identity Column in DocumentDB, the id field is special in that if a value isn't provided by the application - DocumentDB will assign a GUID automatically.

If you need the auto-increment functionality, one potential solution would be to store a counter as a document and leverage DocumentDB's triggers to populate your field and update the counter.