我希望将一个具有默认值的属性添加到我通过SELECT查询检索的一组文档中,如果它们不包含任何值.
我分两部分考虑这个问题:
我想找到article.details.x 不存在的所有文章.
我希望通过Azure门户可以支持这个EXEC命令,所以我不必创建一个迁移工具来运行此命令,但我在门户网站中找不到此选项.这可能吗?
DocumentDB无法在单个查询中更新大量文档.但是,门户网站确实有一个脚本资源管理器,允许您针对单个集合编写和执行存储过程.下面是一个示例sproc,它将查询与replaceDocument命令组合在一起,以更新一些文档,您可以将这些文档用作编写自己的文档的起点.需要牢记的是,DocumentDB不允许sprocs运行超过5秒(使用一些缓冲区).所以你可能需要多次运行你的sproc并跟踪你已经完成的事情,如果它在一个5秒的运行中无法完成.在您的查询中使用IS_DEFINED(collection.field.subfield)!= true(感谢@cnaegle)后跟定义该字段的文档替换(或删除该文档)应该允许您根据需要多次运行sproc .
如果您不想编写sproc,最简单的方法是使用DocumentDB数据迁移工具导出数据库.将其导入Excel以操作或编写脚本以进行操作.然后使用数据迁移工具再次上传它.
您可以使用Azure Document DB Studio作为创建和执行存储过程的前端.它可以在这里找到.它很容易设置和使用.
我根据你的例子模拟了一个存储过程:
function updateArticlesDetailsX() {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var docCount = 0;
var counter = 0;
tryQueryAndUpdate();
function tryQueryAndUpdate(continuation) {
var query = {
query: "select * from root r where IS_DEFINED(r.details.x) != true"
};
var requestOptions = {
continuation: continuation
};
var isAccepted =
collection
.queryDocuments(collectionLink,
query,
requestOptions,
function queryCallback(err, documents, responseOptions) {
if (err) throw err;
if (documents.length > 0) {
// If at least one document is found, update it.
docCount = documents.length;
for (var i=0; i<docCount; i++){
tryUpdate(documents[i]);
}
response.setBody("Updated " + docCount + " documents");
}
else if (responseOptions.continuation) {
// Else if the query came back empty, but with a continuation token;
// repeat the query w/ the token.
tryQueryAndUpdate(responseOptions.continuation);
} else {
throw new Error("Document not found.");
}
});
if (!isAccepted) {
throw new Error("The stored procedure timed out");
}
}
function tryUpdate(document) {
//Optimistic concurrency control via HTTP ETag.
var requestOptions = { etag: document._etag };
//Update statement goes here:
document.details.x = "some new value";
var isAccepted = collection
.replaceDocument(document._self,
document,
requestOptions,
function replaceCallback(err, updatedDocument, responseOptions) {
if (err) throw err;
counter++;
});
// If we hit execution bounds - throw an exception.
if (!isAccepted) {
throw new Error("The stored procedure timed out");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从GitHub上的 Andrew Liu那里得到了这段代码的粗略轮廓.
这个大纲应该接近你需要做的.
| 归档时间: |
|
| 查看次数: |
7109 次 |
| 最近记录: |