Sign In Register

How can we help you today?

Start a new topic
Answered

how to update specific nested json object?


{
 "_id": {
  "$oid": "58106410135f38fb1edad7b1"
 },
 "playerID": "5810640ff43e6005604a6a59",
 
	"OGW_Player_data": {
 
						 "playerName": "tom11"
						 ,
						 "weaponInfo": 
									 {
									  "primaryWeapon": pistol,
									  "secondaryWeapon": knife,
									  "invetoryWeapon": grandae
									 }
						 ,
						 "serverInfo": 
									 {
									  "country": "BD",
									  "latitide": 23.699996948242188,
									  "city": null,
									  "longditute": 90.375
									 }
						 ,
						 
						 "RecordsONDifferentServers":
													[
														{"serverIp":"224.21.242.544","kills":3,"point":33},
														{"serverIp":"224.221.221.11","kills":45,"point":133}
													]
 
					}
} 

    i have that data configuration in my playerData. Now i want to update only the weaponInfo part. for say new weaponInfo will be something like this :

 

"weaponInfo": 
{
"primaryWeapon": pistol,
"secondaryWeapon": knife,
"invetoryWeapon": grandae
}

 
and here is my event  configuration for that .

juS3IEimVtpm7onCO7FCQxsZt6yk4Jbbfw.jpg



Best Answer

Hi Ahsan,


We have a guide on how to do partial queries and updates to complex JSON objects such as yours. You can find it here. Have a read through that and if you have any further questions just let us know.


Thanks,

Liam




Answer

Hi Ahsan,


We have a guide on how to do partial queries and updates to complex JSON objects such as yours. You can find it here. Have a read through that and if you have any further questions just let us know.


Thanks,

Liam



hi. thanks. very helpful tutorial. helped us so much. i need a little more info. how to add an entry to an array object? and then how to update an existing entry? i need this 2 info for this part of my playerdata:

 

"RecordsONDifferentServers":
													[
														{"serverIp":"224.21.242.544","kills":3,"point":33},
														{"serverIp":"224.221.221.11","kills":45,"point":133}
													]
 

 

ho. thanks. very helpful doc . even for a java script developer. as non javascript developer , it was a life saver . a little more info will be very helpful for us. 1. how to add an entry to an array? 2.then how to update an array entry with new value? i am asking for our this field:

 

"RecordsONDifferentServers":
[
{"serverIp":"224.21.242.544","kills":3,"point":33},
{"serverIp":"224.221.221.11","kills":45,"point":133}
]

 

 i am having feeling that can be achieved by same cloud code of  "PROG_UPDATE"  event . but have to do the input  for PATH   and VAL  in a tricky way.

may be using something like this:


 

{
        "@class": ".LogEventRequest",
        "eventKey": "PROG_UPDATE",
        "PATH": "OGW_Player_data.RecordsONDifferentServers[0]",
        "VAL": {"serverIp":"224.21.242.544","kills":3,"point":33},
        "requestId": "1392895516999"
    }

 if so then this also will work :

 

{
        "@class": ".LogEventRequest",
        "eventKey": "PROG_UPDATE",
        "PATH": "OGW_Player_data.RecordsONDifferentServers[0].serverIp",
        "VAL": "224.21.242.544",
        "requestId": "1392895516999"
    }

 
Right?

Hi Ahsan,


You could do this using dot notation:


     "PATH":"OGW_Player_data.RecordsONDifferentServers.0.serverIp"


Or you could make the process a little more dynamic by adding a few extra parameters to your event. For example, if you wanted to change the element with serverIp:"224.21.24.544" to, we'll say, "224.22.75.540" you could add attributes specifying the field name and value of the element you wish to update:


{ "@class": ".LogEventRequest", "eventKey": "PROG_UPDATE", "PATH": "OGW_Player_data.RecordsONDifferentServers",

"FIELD":"serverIp",

"QUERY": "224.24.242.544,

"VAL": "224.22.75.540",

"requestId": "1392895516999" }


Then in your cloud code, use the new fields to refine the query used in your update:


//query to find player's doc and element in that doc with FIELD value matching QUERY provided

//so in this case, find the element of the array where 'serverIp' = '224.24.242.544'

var queryObj = {"_id":Spark.getPlayer().getPlayerId()}

queryObj[inputPath+"."+inputField]=inputQuery;

//update the element matching the query above to the new value

var updateObj = {};

updateObj[inputPath+".$."+inputField]=inputValue;


collection.update(queryObj,{$set:updateObj},true,false);


Try this out and let us know how it works for you.


Regards,

Vinnie

hi thanks. for the tips to save a array element:

but using this:

  

{
    "@class":   ".LogEventRequest",
    "eventKey": "ogw_saveData_ByPath",
    "PATH": "OGW_Player_data.RecordsONDifferentServers.0.serverIp",
    "VAL":  "222.22.222.222",
    "requestId":    "1479405160_20"
}

it is saving data like this:

 

   "OGW_Player_data": {
     "RecordsONDifferentServers": {
      "0": {
       "serverIp": "222.22.222.222"
      }
     }
    }

 which supposed to be in an array like this:

  

"RecordsONDifferentServers":
[
{"serverIp":"222.22.222.222"}
]

 
any idea?
or any tricks?

i did not try  your   array  field  editing method yet.
i will test it as soon as i will be able to save the array elements/

thanks

hi. sorry for my last comment.
after reading the comment several time then i realized what u r trying to say:

"You could do this using dot notation:"

here you actually suggesting to use dot notation instead of  array.
i am not a java script guy. so I am not sure if dot notation have anyway to return back how many object is in it. but with array we can easily figure out the length of array. plus there are several other handy functions to operate on array.

that why i was more interested on array.
is it possible to go for an array ?
if no then surely dot notation is our only option. 


for example:
if i get the "RecordsONDifferentServers" as an array in scriptData then i will be able to use it something like this one:

Hm0azGDWCzlct0TckKKlgDXM84VIejJXKg.jpg



also those functionality available in c++ too.

i am not sure if i will get those  advantages with dot notation.

Login to post a comment