satish kumar V satish kumar V - 2 months ago 17
C# Question

How to create a auto incremented column in Documentdb

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 Source

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.