如何使用documentDB创建唯一索引 [英] How to create a unique index with documentDB

查看:129
本文介绍了如何使用documentDB创建唯一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自使用mongoDB,因此将以此为例,在其中我可以做类似的事情.

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 });

然后每当我尝试插入具有相同ID的数据时,我都会收到重复的键错误,它将无法正常工作.很棒!

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

但是对documentDB可以做类似的事情吗?

But can a similar thing be done with documentDB?

当前,我用node.js编写了以下代码来插入数据:

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);
});

推荐答案

DocumentDB不具有定义唯一约束的本机支持. id属性是一个特殊的例外,它是文档的主键,并且必须唯一.

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.

您可以利用DocumentDB的触发来实施对其他文档属性的唯一约束.我在Github上有一个示例触发器,此处.

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.

这篇关于如何使用documentDB创建唯一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆