I've been following along the javascript stored proc examples shown here
The code below is an attempt at writing a modified version of the update stored proc sample. Here's what I'm trying to do:
- Instead of operating on a single document, I'd like to perform the update on the set of documents returned by a provided query.
- (Optional) Return a count of updated documents in the response body.
Here's the code:
function updateSproc(query, update) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var responseBody = {
updated: 0,
continuation: false
};
// Validate input.
if (!query) throw new Error("The query is undefined or null.");
if (!update) throw new Error("The update is undefined or null.");
tryQueryAndUpdate();
// Recursively queries for a document by id w/ support for continuation tokens.
// Calls tryUpdate(document) as soon as the query returns a document.
function tryQueryAndUpdate(continuation) {
var requestOptions = {continuation: continuation};
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {
if (err) throw err;
if (documents.length > 0) {
tryUpdate(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 {
// Else if there are no more documents and no continuation token - we are finished updating documents.
responseBody.continuation = false;
response.setBody(responseBody);
}
});
// If we hit execution bounds - return continuation:true
if (!isAccepted) {
response.setBody(responseBody);
}
}
// Updates the supplied document according to the update object passed in to the sproc.
function tryUpdate(documents) {
if (documents.length > 0) {
var requestOptions = {etag: documents[0]._etag};
// Rename!
rename(documents[0], update);
// Update the document.
var isAccepted = collection.replaceDocument(
documents[0]._self,
documents[0],
requestOptions,
function (err, updatedDocument, responseOptions) {
if (err) throw err;
responseBody.updated++;
documents.shift();
// Try updating the next document in the array.
tryUpdate(documents);
}
);
if (!isAccepted) {
response.setBody(responseBody);
}
}
else {
tryQueryAndUpdate();
}
}
// The $rename operator renames a field.
function rename(document, update) {
var fields, i, existingFieldName, newFieldName;
if (update.$rename) {
fields = Object.keys(update.$rename);
for (i = 0; i < fields.length; i++) {
existingFieldName = fields[i];
newFieldName = update.$rename[fields[i]];
if (existingFieldName == newFieldName) {
throw new Error("Bad $rename parameter: The new field name must differ from the existing field name.")
} else if (document[existingFieldName]) {
// If the field exists, set/overwrite the new field name and unset the existing field name.
document[newFieldName] = document[existingFieldName];
delete document[existingFieldName];
} else {
// Otherwise this is a noop.
}
}
}
}
}
I'm running this sproc via the azure web portal, and these are my input parameters:
- SELECT * FROM root r
- {$rename: {A: "B"}}
My documents look something like this:
{ id: someId, A: "ChangeThisField" }
After the field rename, I would like them to look like this:
{ id: someId, B: "ChangeThisField" }
I'm trying to debug two issues with this code:
- The updated count is wildly inaccurate. I suspect I'm doing something really stupid with the continuation token - part of the problem is that I'm not really sure about what to do with it.
- The rename itself is not occurring.
console.log()
debugging shows that I'm never getting into theif (update.$rename)
block in the rename function.