1

My Collection is having this type of document.

{
        "id": "65f7a7e9-dc99-4c3a-b9ed-6291a8986343",
        "Username": "[email protected]",
        "Password": "somepassword",
        "FullName": "Abhishek Gupta",
        "DateCreated": "2018-02-18T02:20:50.36367Z",
        "Type": "User",
        "WishList": [
            "18a67f77-5b1b-42f9-8361-bf481fe3a341",
            "3baf9376-1e7f-43af-bd15-0fb317edd95f",
            " << Add New Item HEre  >>"

        ]
    }

I simply want to add one more guid in WishList Array of id .

What is the efficient way to do this rather then updating whole document.

2 Answers 2

2

With the DocumentDB API, you could use a stored procedure to accomplish atomically adding a single item to the array. Stored procedures run within a transaction on the server. Here's a sample that implements something similar to the MongoDB 'update' command:

https://github.com/Azure/azure-documentdb-js-server/blob/master/samples/stored-procedures/update.js

Indeed, there are lots of fun samples here: https://github.com/Azure/azure-documentdb-js-server

Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming you're using the DocumentDB (SQL) API. That being the case: there is no "update" command or query syntax. Rather, you have to read your document, update it yourself (e.g. modifying your JSON array), and replace the document.

The only way to avoid this type of modification would be to move your array elements to separate documents that you would need to link somehow (like with an id in the separate wishlist document, referencing your primary document's id, 65f7a7e9-dc99-4c3a-b9ed-6291a8986343 in your example).

Note: If you're using the MongoDB API, then you can use the native MongoDB update() command, such as:

db.coll.update( {username:"[email protected]"},
 { $push: { WishList: "NewWishListId" } })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.