I'm very new to JSON, so I'm not sure if I've phrased the quest correctly, but what I'm trying to do is store my player inventory data as a JSON object which I then update in the cloud.
I've followed the tutorial How to do partial queries and updates to complex JSON document and that part is working fine, I can create the document and add/change data in the document, but what I can't figure out is how to delete and entry in the inventory.
To illustrate what I mean, here is the JSON string for my player inventory as return by the Query Inventory code in the above tutorial.
And what I'd like to be able to do is delete a complete slot entry without having to replace the entire document, for example if I delete Slot_4 the result would be this.
var playerInventory = Spark.runtimeCollection("player_inventory");
var slotIndex = Spark.data.INDEX;
var slot = "SLOT_" + slotIndex.toString() + "." + Spark.data.FIELD;
var inputValue = Spark.data.VAL;
var updateObject = {};
updateObject[slot] = inputValue;
//Now do the mongo update.
playerInventory.update(
{"_id": Spark.getPlayer().getPlayerId()}, //Looks for a doc with the _id of the current player
{"$set" : updateObject}, // Uses the $set mongo modifier to set value at a path
true, // Create the document if it does not exist (upsert)
false // This query will only affect a single object (multi)
);
Spark.setScriptData("data", slot + " new value = " + inputValue);
I've tried using the DropIndexByName function as so...
var playerInventory = Spark.runtimeCollection("player_inventory");
playerInventory.
var slotIndex = Spark.data.INDEX;
var slot = "SLOT_" + slotIndex.toString();
playerInventory.dropIndexByName(slot);
Spark.setScriptData("data", "Cleared contents of " + slot);
but I get an error 27 index not found with name [SLOT_0], so I guess I've misunderstood things somewhat.
I'd be very grateful if someone would be able to provide an example or some pointers as to how I would handle the cloud code for this.
Many thanks
Martin
Best Answer
C
Customer Support
said
about 6 years ago
Hi Martin,
If you see my second reply, there is actually a more efficient and straight forward implementation.
But to answer your question here is a pseudo example:
var newObject = {}; var oldObject = // document returned from query as an object
// oldObject is like so {"key1": "value1, "key2" : "value2", "key3": "value3"};
//construct new object without unwanted values
newobject.newKey1 = oldObject.key1;
newObject.newKey2 = oldObject.key2;
// newObject contains key1 and key 2 but not key3.
This is a very basic example but hopefully you get my meaning. That being said best practice is to use the $unset modifier which I linked you to above.
I've just realised there is an extra line (line 2) in the last code example above, the actual code I'm using is...
var playerInventory = Spark.runtimeCollection("player_inventory");
var slotIndex = Spark.data.INDEX;
var slot = "SLOT_" + slotIndex.toString();
playerInventory.dropIndexByName(slot);
Spark.setScriptData("data", "Cleared contents of " + slot);
Customer Support
said
about 6 years ago
Hi Martin,
It's actually more straight forward than you may think.
The method I would use is the following:
Query the doc you wish to operate upon, in this case a players inventory.
Reference it as an object by assigning the "findOne" query to a variable. (This will be the object)
Manipulate the object variable values or keys by constructing a new object with the variables you want to keep, in this case maybe slots 1 and 2.
update the doc with the same query only with the new object.
Bear in mind you would have to query the object again if you wish to reference it in its new state.
Does this make sense?
Happy to assist further.
Best regards, Patrick.
Customer Support
said
about 6 years ago
Hi again Martin,
I've actually given you an inefficient example which I will leave as a comparison.
Mongo actually provides an operator explicitly for the purpose you described above. This is $unset and allows you to reference potential fields for removal.
thanks for replying. I understand the principal of what you're suggesting, however I'm very inexperienced with JSON (I'd never used it up until a couple of days ago), and I may still struggle to turn 'Manipulate the object variable values or keys by constructing a new object with the variables you want to keep, in this case maybe slots 1 and 2' into working cloud code. (if you were talking about a C# typed list or something similar instead it would be easy for me to do.
However, I'll try a few things and see if I can get my head around it, and if I get completely stuck I'll post here again for more help if you don't mind.
Thanks
Martin.
M
Martin Horn
said
about 6 years ago
Hi again,
this is just to acknowledge your second post which I didn't see until after my last reply. I'll check out the unset command and see if I can get that working how I want.
Thanks again
Martin.
Customer Support
said
about 6 years ago
Answer
Hi Martin,
If you see my second reply, there is actually a more efficient and straight forward implementation.
But to answer your question here is a pseudo example:
var newObject = {}; var oldObject = // document returned from query as an object
// oldObject is like so {"key1": "value1, "key2" : "value2", "key3": "value3"};
//construct new object without unwanted values
newobject.newKey1 = oldObject.key1;
newObject.newKey2 = oldObject.key2;
// newObject contains key1 and key 2 but not key3.
This is a very basic example but hopefully you get my meaning. That being said best practice is to use the $unset modifier which I linked you to above.
Hope this helps.
Best regards, Patrick.
1 person likes this
M
Martin Horn
said
about 6 years ago
Hi again Patrick,
thanks for the example, whilst as you say it isn't the best way to approach it, in terms of my learning how to manipulate JSON it is still very useful to me.
But in the meantime I've achieved my goal using the unset function as you suggested, with this cloud code, the fact that it works leads me to assume I have taken the correct approach, however I'll be happy to be instructed otherwise.
var playerInventory = Spark.runtimeCollection("player_inventory");
var slotIndex = Spark.data.INDEX;
var slot = "SLOT_" + slotIndex.toString();
var inputValue = Spark.data.VALUE;
var updateObject = {};
updateObject[slot] = slot;
//Now do the mongo update.
playerInventory.update(
{"_id": Spark.getPlayer().getPlayerId()}, //Looks for a doc with the _id of the current player
{"$unset" : updateObject}, // Uses the $set mongo modifier to set value at a path
true, // Create the document if it does not exist (upsert)
false // This query will only affect a single object (multi)
);
Spark.setScriptData("data", "Cleared contents of " + slot);
Many thanks for the fast and informative replies, very much appreciated.
Martin.
M
Martin Horn
said
about 6 years ago
I've just noticed that I left the var inputValue = Spark.data.VALUE; in the code by accident.
Which raises another question, I don't appear to be able to edit my posts, is there a way to do that?
Martin Horn
Hi,
I'm very new to JSON, so I'm not sure if I've phrased the quest correctly, but what I'm trying to do is store my player inventory data as a JSON object which I then update in the cloud.
I've followed the tutorial How to do partial queries and updates to complex JSON document and that part is working fine, I can create the document and add/change data in the document, but what I can't figure out is how to delete and entry in the inventory.
To illustrate what I mean, here is the JSON string for my player inventory as return by the Query Inventory code in the above tutorial.
And what I'd like to be able to do is delete a complete slot entry without having to replace the entire document, for example if I delete Slot_4 the result would be this.
This is my code for updating the inventory
I've tried using the DropIndexByName function as so...
but I get an error 27 index not found with name [SLOT_0], so I guess I've misunderstood things somewhat.
I'd be very grateful if someone would be able to provide an example or some pointers as to how I would handle the cloud code for this.
Many thanks
Martin
Hi Martin,
If you see my second reply, there is actually a more efficient and straight forward implementation.
But to answer your question here is a pseudo example:
var newObject = {};
var oldObject = // document returned from query as an object
// oldObject is like so {"key1": "value1, "key2" : "value2", "key3": "value3"};
//construct new object without unwanted values
newobject.newKey1 = oldObject.key1;
newObject.newKey2 = oldObject.key2;
// newObject contains key1 and key 2 but not key3.
This is a very basic example but hopefully you get my meaning. That being said best practice is to use the $unset modifier which I linked you to above.
Hope this helps.
Best regards, Patrick.
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstMartin Horn
I've just realised there is an extra line (line 2) in the last code example above, the actual code I'm using is...
Customer Support
Hi Martin,
It's actually more straight forward than you may think.
The method I would use is the following:
Customer Support
Hi again Martin,
I've actually given you an inefficient example which I will leave as a comparison.
Mongo actually provides an operator explicitly for the purpose you described above. This is $unset and allows you to reference potential fields for removal.
You can find documentation on this feature here: https://docs.mongodb.com/manual/reference/operator/update/unset/
Happy to answer any other questions you have.
Best regards, Patrick.
1 person likes this
Martin Horn
Hi Patrick,
thanks for replying. I understand the principal of what you're suggesting, however I'm very inexperienced with JSON (I'd never used it up until a couple of days ago), and I may still struggle to turn 'Manipulate the object variable values or keys by constructing a new object with the variables you want to keep, in this case maybe slots 1 and 2' into working cloud code. (if you were talking about a C# typed list or something similar instead it would be easy for me to do.
However, I'll try a few things and see if I can get my head around it, and if I get completely stuck I'll post here again for more help if you don't mind.
Thanks
Martin.
Martin Horn
Hi again,
this is just to acknowledge your second post which I didn't see until after my last reply. I'll check out the unset command and see if I can get that working how I want.
Thanks again
Martin.
Customer Support
Hi Martin,
If you see my second reply, there is actually a more efficient and straight forward implementation.
But to answer your question here is a pseudo example:
var newObject = {};
var oldObject = // document returned from query as an object
// oldObject is like so {"key1": "value1, "key2" : "value2", "key3": "value3"};
//construct new object without unwanted values
newobject.newKey1 = oldObject.key1;
newObject.newKey2 = oldObject.key2;
// newObject contains key1 and key 2 but not key3.
This is a very basic example but hopefully you get my meaning. That being said best practice is to use the $unset modifier which I linked you to above.
Hope this helps.
Best regards, Patrick.
1 person likes this
Martin Horn
Hi again Patrick,
thanks for the example, whilst as you say it isn't the best way to approach it, in terms of my learning how to manipulate JSON it is still very useful to me.
But in the meantime I've achieved my goal using the unset function as you suggested, with this cloud code, the fact that it works leads me to assume I have taken the correct approach, however I'll be happy to be instructed otherwise.
Many thanks for the fast and informative replies, very much appreciated.
Martin.
Martin Horn
I've just noticed that I left the var inputValue = Spark.data.VALUE; in the code by accident.
Which raises another question, I don't appear to be able to edit my posts, is there a way to do that?
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics