Storage Service SaveOrUpdateDocumentByKeyValue and UpdateDocumentByKeyValue operation?

0 votes

1. I create a database entry with:

String JSONstr =  "{\"emailId\":\""+email+"\",\"expirationdate\":\""+expDateString+"\"}";
storageService.InsertJSONDocument(dbNamecollectionNameJSONstrnew App42CallBack());

2. Then later say I want to update ONLY the expirationdate using 'SaveOrUpdateDocumentByKeyValue()'  or  'UpdateDocumentByKeyValue()'...

3. Do I have to supply the complete JSONstr that was used to create this entry again as:


String JSONstr =  "{\"emailId\":\""+email+"\",\"expirationdate\":\""+newExpDateString+"\"}";

4. Or can I use a shorter string with just the expirationdate key/value I want to change as:

String JSONstr =  "{\"expirationdate\":\""+newExpDateString+"\"}";

 
5. If I use the shorter version will that overwrite (effectively deleting) the emailId key/value ?
 
p.s. I think I tried it with the short version and the emailId key/value WAS LOST.
 
asked Aug 21, 2014 in App42 Cloud API-BaaS by huffman (50 points)

1 Answer

0 votes

Hello Huffman,

Thanks for sharing the detail and use case with us.

In save or update method you need to pass the full json string which you want to update. We will recommend you that you can use addOrUpdateKeys method, if you want to add/update some values in jsondoc. Kindly find the below code snippet for add/update key values:

 

String dbName = "<Your_DataBase_Name>";

String collectionName = "<Your_Collection_Name>"; 

String docId = "docId";

String keys = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}";

storageService.AddOrUpdateKeys(dbName, collectionName, docId, keys, new UnityCallBack()); 

//This will update/add name and age key in given doc.

image

public class UnityCallBack : App42CallBack

{

public void OnSuccess(object response)

{

Storage storage = (Storage) response; 

App42Log.Console("dbName is " + storage.GetDbName());

App42Log.Console("collection Name is " + storage.GetCollectionName());

IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); 

for(int i=0;i <jsonDocList.Count;i++)

{   

App42Log.Console("objectId is " + jsonDocList[i].GetDocId());

App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());

App42Log.Console("CreatedAt is : " + jsonDocList[i].GetCreatedAt());

App42Log.Console("UpdatedAt is : " + jsonDocList[i].GetUpdatedAt());

}  

}

 

public void OnException(Exception e)

{

App42Log.Console("Exception : " + e);

}

}

 

Let us know if it helps.

Thanks. 

answered Aug 21, 2014 by hs00105 (2,005 points)
I tried this example but it doesn't update anything. To make sure that the rest of my code is correct I substituted the addOrUpdateKeys() call with updateDocumentByDocId() which uses the same arguments. addOrUpdateKeys() returns an error while updateDocumentByDocId() returns success, updates the specified key/value but it wipes out the other keys/values in my database JSON string.
Hello Huffman,

Could you please share the exception message which you are having in addOrUpdateKeys method. It will helps us to provide better support to you.
Thanks,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...