Azure / azure-cosmosdb-js-server

The JavaScript SDK for server-side programming in Azure Cosmos DB
MIT License
178 stars 153 forks source link

Examples for update.js using C# .NET SDK #16

Open hum-bug opened 7 years ago

hum-bug commented 7 years ago

Hi,

I'm trying to use the stored procedure update.js from a c# .NET client using the following:

string[] spparams = new string[2] { docID, @"$addToSet: {arrayname: {foo1:\""bar1\"", foo2:\""bar2\""}" };

var response = await dbclient.ExecuteStoredProcedureAsync<string>(UriFactory.CreateStoredProcedureUri(_appSettings.DBID, _appSettings.CollectionID, sproc), spparams);

It seems to run without error but just returns the original document - no changes.

I'm not sure I'm submitting the correct structure but when I test this in the portal it runs correctly but again, no changes made to the data in the document. Just the original document returned.

["5b22f104-a943-98f9-14dd-947e79a17480","$addToSet: {arrayname: {foo1:\"bar1\",foo2:\"bar2\"}"]

I also tried a more simple $set but see same issues.

Is there any .NET examples for using $addToSet in update.js?

Thanks

rnagpal commented 7 years ago

@mkolt : Can you help here?

mkolt commented 7 years ago

You need to pass 2nd parameter as object and not string, as the sproc expects an object. Try something like this: var spParams = new object[2] { docID, JObject.Parse(@"$addToSet: {arrayname: {foo1:\""bar1\"", foo2:\""bar2\""}") };

hum-bug commented 7 years ago

That was the issue.

For anyone reading this with similar issue - I tested using the following:

var spParams = new object[2] { docID, JObject.Parse(@"{""$addToSet"": {""arrayname"": {""foo1"":""bar1"", ""foo2"":""bar2""}}}") };

I also added using Newtonsoft.Json.Linq;

Make sense now I know how. As you said I was expecting to pass a JSON string so will plan out using a model.

Appreciate the quick response - thanks.