question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_ts is not returned by upsertDocument or replaceDocument

See original GitHub issue

I have written a Stored Procedure to do a partial Document update and I want to return only the _ts attribute in order for subsequent queries to use this as a parameter.

This was tested in the Azure Portal Data Explorer and using the node.js sdk, both had the same result.

Whether I use upsertDocument or replaceDocument these are the only four system attributes returned in the RequestCallback.resource:

"_rid": "0eUiAJMAdQDl9QAAAAAAAA==",
"_self": "dbs/0eUiAA==/colls/0eUiAJMAdQA=/docs/0eUiAJMAdQDl9QAAAAAAAA==/",
"_etag": "\"27014d93-0000-0000-0000-5b7ba4ae0000\"",
"_attachments": "attachments/"

The Document itself is updated properly by the Stored Procedure, and the changes (including the new _ts) can be verified immediately in Data Explorer.

I also tried executing a small query after the upsert/replace and even this doesn’t work inside the same Stored Procedure:

SELECT c._ts FROM c WHERE c.id='f21d829d-2de5-0a27-8886-ff2c3ddb2119'

Return value from Stored Procedure:

[
    {}
]

Result in Data Explorer (run as SQL Query):

[
    {
        "_ts": 1534831246
    }
]

Stored Procedure code:

function UpdatePartial(id, update, rootNode){

    var context = getContext();
    var collection = context.getCollection();

    var query = `SELECT * FROM c WHERE c.id='${id}'`;

    var queryAccepted = collection.queryDocuments(collection.getSelfLink(), query, {}, onQueryResponse);
    if(!queryAccepted) throw "Unable to query DocumentDB";

    function onQueryResponse(err, documents, responseOptions) {

        if(err){
            throw err;
        }

        if(documents.length === 0){
            throw `Could not find document with id [${id}]`;
        }

        var source = documents[0];
        update = JSON.parse(update);

        if(rootNode){
            source[rootNode] = Merge(source[rootNode], update);
        } else {
            source = Merge(source, update);
        }

        var updateAccepted = collection.replaceDocument(source._self, source, onUpdateResponse);
        if(!updateAccepted) throw "Unable to update DocumentDB";

    }

    function onUpdateResponse(err, resource, options){

        if(err){
            throw err;
        }

        context.getResponse().setBody({"_ts": resource._ts || ''});

        // use this to return the entire document instead
        // context.getResponse().setBody(resource);

        // uncomment these lines to execute the query
        // var query = `SELECT c._ts FROM c WHERE c.id='${id}'`;
        // console.log(query);
        // collection.queryDocuments(collection.getSelfLink(), query, onTimeStampResponse);

    }

    function onTimeStampResponse(err, resource){

        if(err){
            throw err;
        }

        context.getResponse().setBody(resource);      

    }

    
    function Merge(source, update) {

        for (var key in update) {
        
            try {
        
                if ( update[key].constructor==Object ) {
                    source[key] = Merge(source[key], update[key]);
                } else {
                    source[key] = update[key];
                }
        
            } catch(err) {
                source[key] = update[key];
            }
        
        }

        return source;

    }


}

Here is the node.js code that calls the Stored Procedure:

exports.executeSproc = function(id, update, callback){

  let url = getCollectionUrl(config.database, config.collection) + '/sprocs/' + 'UpdatePartial';
  let options = [id, update];
  
  client.executeStoredProcedure(url, options, function(err, resource){

    if(err){
      console.error(`ERROR in documentdb.executeSproc\nid was:${id}\nUpdate was: ${JSON.stringify(update)}\nError:\n${JSON.stringify(err,null,2)}`);
      callback(err);
      return;
    }

    callback(resource);

  });

}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

2reactions
yulang-wangcommented, Mar 21, 2019

+1. This issue also appear in createDocument

image

0reactions
drago-draganovcommented, May 23, 2022

It’s great indeed to see a response 3 years later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cosmos DB Server-Side Programming with TypeScript – Part 4
In summary, we can retrieve documents by their ID ( readDocument() ), perform queries on the documents in the collection ( queryDocuments() ), ......
Read more >
DocumentDB updating multiple documents fails - Stack Overflow
Based on the response returned I can see that the query returns 100 documents. tryUpdate is called twice but the second call to...
Read more >
documentdb - Go Packages
Continuation a string token returned for queries and read-feed ... func (c *DocumentDB) ReplaceDocument(link string, doc interface{}, opts .
Read more >
Partial document update in Azure Cosmos DB - Microsoft Learn
If the target path specifies an element that does not exist, ... the full document is returned even if the update is triggered...
Read more >
Using TypeScript to write Cosmos DB stored procedures with ...
isAccepted) reject(new Error(429, "queryDocuments was not accepted.")); }); } function replaceDocument(doc, options) { return new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found