1
votes

I have a JSON document in DocumentDB.

I wish to add new data to a given document.

For example.

Current document:

{
    "User": "ABC",
    "UserID": "123"
}

New document

{
    "User": "ABC",
    "Height":"1.60",
    "UserID": "123"
}

I have tried solutions such as mentioned here:

Add new properties to DocumentDB Document in Stored procedure

But I get "getContext is not defined".

I believe the issue is that I am unsure of how to call a document via a function.

I would like:

var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docsname'"
editDocument(documentURL)

function editDocument(document) {
    var collection = getContext().getCollection();
    var response = getContext().getResponse();

    document.Height = "1.60";

    collection.createDocument(collection.getSelfLink(), document, function(err, result) {
    if(err) throw err;

       // Return the resulting document back as the response.
       response.setBody(result);
    });
}

Should I use client.replaceDocument instead, or maybe there is a better way to do this.

The issue may be that the function is plainly trying to use the text location "dbs/colls/docs" in the function rather than the document itself.

Thanks for any help in advance!

1
You need to actually upload the stored procedure to Cosmos and call it there. getContext is specific to that runtime environment you can't just run it in a random node script - Jesse Carter
I see, is there a way to edit documents without stored procedures? - JDT
No, unless you use a Stored Procedure you're only able to Create, Upsert, or Replace entire documents. You won't be able to modify individual properties - Jesse Carter

1 Answers

0
votes

Yes, you need to use replaceDocument instead.

var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docid'"

client.readDocument(documentURL, (err, doc) => {
    if(err) return console.log(err);

    doc.Height = "1.60";

    client.replaceDocument(documentUrl, doc, (err, result) => {
        if(err) return console.log(err);
        console.log('replaced document');
    });
});