我打算更新文档,如果文档的其他记录中已经存在类似的值。
假设,如果根文档中几个字段(轨道,时间)相似的记录很少,那么我想将isOriginal字段更新为false记录。
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 ORDER BY r.created.epoch DESC"
};
var requestOptions = {
continuation: continuation
};
var isAccepted =
collection
.queryDocuments(collectionLink,
query,
requestOptions,
function queryCallback(err, documents, responseOptions) {
//response.setBody(responseOptions);
if (err) throw err;
if (documents.length > 0) {
document.upd
// If at least one document is found, update it.
docCount = documents.length;
//response.setBody("Found " + docCount + " documents");
for (var i=0; i<docCount; i++){
tryUpdate(documents[i]);
}
//response.setBody("Updated " + docCount + " documents");
}
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.update({"track":{document.track},"Time":{document.Time} }, {"$set":{"isOriginal":"false!"}});
document.created = {
date: "2016-06-22 19:18:14",
epoch: 1466623094582
}
//response.setBody(document);
var isAccepted = collection
.replaceDocument(document._self,
document,
requestOptions,
function replaceCallback(err, updatedDocument, responseOptions) {
if (err) throw err;
counter++;
response.setBody("Updated " + counter + " documents");
// response.setBody(updatedDocument);
});
// If we hit execution bounds - throw an exception.
if (!isAccepted) {
//throw new Error("The stored procedure timed out");
}
}
}