更新 Azure 文档 DB 存储过程中的另一个集合

Kev*_*zyk 3 javascript stored-procedures azure azure-cosmosdb

我们需要更新集合中的所有文档以改变它们的形状。我们想在不同的集合中记录更改的审计,我们将在其中存储包含旧版本和新版本的文档。为了使这个更改原子化,我们使用了一个存储过程。

我们面临的问题是从存储过程更新另一个集合。似乎审计文档总是写入存储过程所属的集合中。

我写了一个示例存储过程:

function sample(prefix) {
    var context = getContext();
    var fooCollection = context.getCollection();
    var barCollection = context.getCollection("BarCollection");

    // Query documents and take 1st item.
    var isAccepted = fooCollection.queryDocuments(
        fooCollection.getSelfLink(),
        'SELECT * FROM root r',
        function (err, feed, options) {
            if (err) throw err;

            // Check the feed and if empty, set the body to 'no docs found', 
            // else take 1st element from feed
            if (!feed || !feed.length) context.getResponse().setBody('no docs found');
            else {
                var fooDoc = feed[0];

                var barDoc = {
                    "foo" : fooDoc,
                    "bar" : "bar"
                }

                var isAccepted2 = barCollection.createDocument(barCollection.getSelfLink(), barDoc);  

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

        });

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

在此示例中,我的存储过程保存在FooCollection. 我得到一个文档并尝试将副本保存到BarCollection. 新文档始终保存在FooCollection.

Document DB 是否支持此方案?如果是这样,我需要进行哪些更改才能使存储过程正常工作?

Dav*_*gon 5

DocumentDB 存储过程是集合范围的。您将无法将审计信息写入单独的集合;您需要写入同一个集合。