Update Document By Key Value WIth JSON Object in AS3

0 votes

I am newbie and I need some help. I have a JSON document in NoSQL DB  {"Bullets":20} and when I am trying to update it with this code below - I am getting this message Exception Message Error: {"app42Fault":{"httpErrorCode":404,"appErrorCode":2601,"message":"Not Found","details":"Document by key 'Bullets' and value '20' does not exist."}} 

CODE:

 

var dbName:String = "VORTEX";

var collectionName:String = "UserData";

var key:String = "Bullets";
var Value = 20;
var newJsonDoc:Object  = new Object();
newJsonDoc.Bullets = 15;
storageService.updateDocumentByKeyValueWithJsonObject (dbName,collectionName,key,Value ,newJsonDoc ,new JSONUpdateCallBack());
Help please!
asked Aug 12, 2014 in App42 Cloud API-BaaS by harry.sky.vortex (16 points)

1 Answer

+1 vote
 
Best answer

Hi Harry,

Thanks for writing to us.

Kindly use the below code snippet for updating the document in storage service:

var dbName:String  = "<Your_DataBase_Name>";

var collectionName:String = "<Your_Collection_Name>";   

var key:String = "age";  

var value:int = 30;    

var query:Query = QueryBuilder.build(key, value, Operator.GREATER_THAN);  

var newJsonDoc:Object  = new Object();

newJsonDoc.name = "John"; 

newJsonDoc.age = 20; 

storageService.updateDocumentByQuery(dbName, collectionName, query ,newJsonDoc ,new callback());

class callback implements App42CallBack  

{  

public function onException(exception:App42Exception):void  

{  

trace("Exception Message " + exception);  

}  

public function onSuccess(response:Object):void  

{  

var storage:Storage = Storage(response);

var jsonDoc:JSONDocument = new JSONDocument(); 

trace("dbName is " + storage.getDbName());

trace("Collection Name is :  " + storage.getCollectionName());

var jsonDocsList:Array = storage.getJsonDocList();

for(var i:int=0; i < jsonDocsList.length;i++)

{

jsonDoc = JSONDocument(jsonDocsList[i]);

trace("Docid is : " + jsonDoc.getDocId());

trace("CreatedAt is : " + jsonDoc.getCreatedAt());

trace("UpdatedAt is :  " + jsonDoc.getUpdatedAt());

trace("JsonDoc is :  " + jsonDoc.getJsonDoc());

}

}  

}

Let me know if it helps.

Thanks

 

 

answered Aug 12, 2014 by hs00105 (2,005 points)
selected Aug 12, 2014 by harry.sky.vortex
Our updateDocumentByKeyValueWithJsonObject method, support only string values in input parameter e.g: If you want to update your document where values is other that String you can use our updateDocsByQuery
in which you can update your values base on your given query as shown in above code snippet.
Thanks.
Thanks! It works, but with one correction: var query:Query = QueryBuilder.build(key, value, ------>Operator.EQUALS<------);  But it overwrite JSON document :( Is there any API to just update some variables, not deleting another?
In this case, you need to use addOrUpdateKeys method which will update the json.
Kindly find the link below for addOrUpdateKey method:
http://api.shephertz.com/app42-docs/nosql-storage-service/?sdk=unity#add-or-update-keys-with-json-string
 
Let us know if it helps.
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
...