Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a unique index with documentDB

Im coming from using mongoDB so will use this as an example, in it I can do something like.

collection.ensureIndex('id', {unique: true, dropDups: true });

And then whenever I try to insert data with the same id, I will get a duplicate key error and it wont work. great!

But can a similar thing be done with documentDB?

Currently I have the following code written with node.js to insert data:

client.createDocument(collection_id, data_to_insert, function (err, doc) {
    callback(err, doc);
});
like image 491
wazzaday Avatar asked Jul 20 '15 14:07

wazzaday


1 Answers

DocumentDB does not have native support for defining unique constraints. The id property is a special exception in which it is the primary key for a document and must be unique.

You can leverage DocumentDB's triggers to implement a unique constraint on other document properties. I have a sample trigger on Github here.

From an indexing perspective - every attribute in every document is automatically indexed by default. If you wish to tune specifically which paths get indexed and which do not, you can define a custom index policy on a collection level.

like image 133
Andrew Liu Avatar answered Oct 19 '22 07:10

Andrew Liu