更新 Azure 文档数据库存储过程中的另一个集合
Update another collection in an Azure Document DB stored procedure
我们需要更新集合中的所有文档以更改它们的形状。我们想在不同的集合中记录对更改的审计,我们将在其中存储包含旧版本和新版本的文档。为了使此更改原子化,我们使用存储过程。
我们面临的问题是从存储过程更新另一个集合。好像审计文件总是写到存储过程所属的集合中。
我写了一个示例存储过程:
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.');
}
在这个示例中,我的存储过程保存在FooCollection
中。我得到一份文件并尝试将副本保存到 BarCollection
。新文档始终保存到 FooCollection
.
Document DB 是否支持这种情况?如果是这样,我需要做哪些更改才能使存储过程正常工作?
DocumentDB 存储过程是集合范围的。您将无法将审核信息写入单独的集合;您需要写入同一个集合。
我们需要更新集合中的所有文档以更改它们的形状。我们想在不同的集合中记录对更改的审计,我们将在其中存储包含旧版本和新版本的文档。为了使此更改原子化,我们使用存储过程。
我们面临的问题是从存储过程更新另一个集合。好像审计文件总是写到存储过程所属的集合中。
我写了一个示例存储过程:
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.');
}
在这个示例中,我的存储过程保存在FooCollection
中。我得到一份文件并尝试将副本保存到 BarCollection
。新文档始终保存到 FooCollection
.
Document DB 是否支持这种情况?如果是这样,我需要做哪些更改才能使存储过程正常工作?
DocumentDB 存储过程是集合范围的。您将无法将审核信息写入单独的集合;您需要写入同一个集合。