我可以检查唯一性并在Cosmos DB存储过程中写文档吗 [英] Can I check for uniqueness and write a document in a Cosmos DB stored procedure

查看:42
本文介绍了我可以检查唯一性并在Cosmos DB存储过程中写文档吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cosmos DB存储过程,如果它不存在,它会创建一个新文档并返回现有的或新的文档.可以从不同的过程调用该过程.我可以确定即使多个客户端试图同时创建该文档也只能创建一次?

I have a Cosmos DB stored procedure that creates a new document if it doesn't already exist and returns the existing or the new document. The procedure may be called from different processes. Can I be sure that the document is only created once even if several clients are trying to create it at the same time?

function createIfNotExists(param1, param2) {
    var collection = getContext().getCollection();

    // Query documents and take 1st item.
    var query = 'SELECT * FROM c WHERE c.Param1="' + param1 + '" AND c.Param2="' + param2 + '"';
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        query,
        function (err, items, options) {
            if (err) throw err;

            var response = getContext().getResponse();
            if (!items || !items.length) {
                var accepted = collection.createDocument(collection.getSelfLink(),
                {
                    Param1: param1,
                    Param2: param2
                },
                function (err, itemCreated) {
                    if (err) throw err;
                    response.setBody(itemCreated);
                });

                if (!accepted) throw new Error('Could not create document');
            }
            else {
                response.setBody(items[0]);
            }
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

推荐答案

我可以确定即使创建了多个文档,该文档也只能创建一次客户正在尝试同时创建它?

Can I be sure that the document is only created once even if several clients are trying to create it at the same time?

Hein,关于您的关注,您可以深入研究文档,它说明了Cosmos DB中的ACID.

Hein, about your concern,you could dig into this document which expounds ACID in Cosmos DB.

在Azure Cosmos DB中,JavaScript运行时托管在数据库中引擎.因此,在存储过程中发出的请求以及触发器在与数据库会话相同的作用域内执行.这该功能使Azure Cosmos DB可以保证所有对象的ACID属性存储过程或触发器的一部分的操作.为了例子.

In Azure Cosmos DB, JavaScript runtime is hosted inside the database engine. Hence, requests made within the stored procedures and the triggers execute in the same scope as the database session. This feature enables Azure Cosmos DB to guarantee ACID properties for all operations that are part of a stored procedure or a trigger. For examples.

因此,我认为您的问题的答案是肯定的,因为原子性保证了事务中完成的所有操作都被视为一个单元,并且所有这些操作都已提交或全部都不提交.

So, i think the answer is yes for your question because the atomicity guarantees that all the operations done inside a transaction are treated as a single unit, and either all of them are committed or none of them are.

顺便说一句,您还可以在创建集合之前设置一个或多个唯一键,请参阅此

BTW,you also could set one or more unique keys before the creation of collection,please refer to this blog.

这篇关于我可以检查唯一性并在Cosmos DB存储过程中写文档吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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