How to create a unique index using documentDB

Im coming from using mongoDB 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 get a duplicate key error and it won't work. great!

But can you do this with documentDB?

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

client.createDocument(collection_id, data_to_insert, function (err, doc) {
    callback(err, doc);
});

      

+3


source to share


2 answers


DocumentDB does not have built-in support for defining unique constraints. Property id

is a special exception in that it is the primary key for the document and must be unique.

You can use DocumentDB triggers to enforce unique constraints on other document properties. I have an example trigger on Github here .



From an indexing perspective, every attribute in every document is automatically indexed by default. If you want to fine-tune which paths are indexed and which are not, you can define a custom index policy at the collection level.

+3


source


Support for unique constraints is now available: https://docs.microsoft.com/en-us/azure/cosmos-db/unique-keys .



+2


source







All Articles