0

I am trying to set the property of all documents of a particular user to false. I am using Stored Procedure to do the same. The Below Procedure fetches only a partial of the records and updates them.

I have a total of 2399 documents. But the Procedure fetches only 1332 and updates them.

function spBulkUpdateTrackInventory(tenantId) {
    var queryDocument = " select * from c where c.tenantId = '" + tenantId + "'";
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    var responseBody = {
        updatedCount: 0,
        continuation: true
    };

    fetchProducts();

    function fetchProducts(continuation) {

        var requestOptions = { continuation: continuation, pageSize:-1};

        var isAccepted = collection.queryDocuments(collection.getSelfLink(), queryDocument, requestOptions,
            function (err, retrievedDocs, responseOptions) {
                if (err) throw new Error("Error" + err.message);

                if (retrievedDocs.length > 0)
                {
                    updateTrackInventory(retrievedDocs, responseOptions.continuation);
                }
            });
        if (!isAccepted) getContext().getResponse().setBody(responseBody);
    }

    function updateTrackInventory(documents, continuation) {
            for (var cnt = 0; cnt < documents.length; cnt++)
            {
                newdocument = documents[cnt];
                newdocument.trackInventory = true;
                responseBody.updatedCount++;
                var isAccepted = collection.replaceDocument(documents[cnt]._self, newdocument);

                if (!isAccepted) {
                    response.setBody(responseBody);
                }
            }

            if (continuation) {
                fetchProducts(continuation);
            }
        responseBody.continuation = false;
        response.setBody(responseBody);
    }
}

What am I missing?

sham
  • 691
  • 8
  • 28

1 Answers1

1

I created 3000 documents which are half-partitioned by name partition key to test your code.It works fine.

enter image description here

I suggest you checking if the SQL result is consistent running in stored procedure and in query shell.In addition, you could follow this case How to debug Azure Cosmos DB Stored Procedures? : to use console.log to debug your stored procedure.

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32