1
votes

I have cosmos-db collection with great size, I want to rename some properties of this collection, I have found that I can implement C# application, which loop on collection documents, and replace them document per document, but this solution will take very long time according to collection size. Is there any other solutions on Azure Portal (functions or stored procedures) or SDKs, which will give us the same function with less time?

Example:

Old document { Code, CName, CAddress }

I want to rename properties to be { CustomerCode, CustomerName, CustomerAddress }

1

1 Answers

1
votes

As you mentioned in the question, you can use Stored Procedure to do that. You can follow the sample code here

  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.
                }
            }
        }
    }