Hi. I'm trying to save player generated entities (buildings, units, sectors), but I need those to have unique ID (preferably created by the platform), so the client only handles random strings. To achieve this, i created a runtime collection and add entities like so:
var dCollection = Spark.runtimeCollection("player_progress");
var json = Spark.data.VALUE;
var entry = {"playerId" : Spark.getPlayer().getPlayerId(), "data":json};
var success = dCollection.insert(entry);
When I want to retrieve an entity, I use:
var dCollection = Spark.runtimeCollection("player_progress");
var docId = Spark.data.ID;
var doc = dCollection.findOne({"_id":{"$oid": docId}});
Spark.setScriptData("item", doc);
Is this the correct approach?
I am trying to modify an entry, with:
var dCollection = Spark.runtimeCollection("player_progress");
var docId = Spark.data.ID;
var field = Spark.data.FIELD;
var doc = dCollection.findAndModify( <missing code to find doc with id and modify field> );
dCollection.save(doc);
Could you please show an example of how to modify the doc with given docId (playerId is irrelevant here) and field. The field is a number and would increase by 1.
Thanks!
Best Answer
P
Pavao Ivancek
said
over 6 years ago
I was able to do it with:
var doc = dCollection.findOne({"_id":{"$oid": docId}});
doc.eData.ProductionPerMin = 2;
var success = dCollection.update({"_id":{"$oid": docId}}, doc);
Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?
var doc = dCollection.findOne({"_id":{"$oid": docId}});
doc.eData.ProductionPerMin = 2;
var success = dCollection.update({"_id":{"$oid": docId}}, doc);
Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?
Customer Support
said
over 4 years ago
Hey Paavo,
Using update is a good alternative, but you should use $set instead of updating the whole doc. This will allow you to update only certain part of the doc rather then the whole thing.
One way you could reduce the amount of data in each doc could be to store separate entities in different collections, but without knowing how you access them and how often its hard to advise.
Pavao Ivancek
Hi. I'm trying to save player generated entities (buildings, units, sectors), but I need those to have unique ID (preferably created by the platform), so the client only handles random strings. To achieve this, i created a runtime collection and add entities like so:
When I want to retrieve an entity, I use:
Is this the correct approach?
I am trying to modify an entry, with:
Could you please show an example of how to modify the doc with given docId (playerId is irrelevant here) and field. The field is a number and would increase by 1.
Thanks!
I was able to do it with:
Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstPavao Ivancek
I was able to do it with:
Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?
Customer Support
Hey Paavo,
Using update is a good alternative, but you should use $set instead of updating the whole doc. This will allow you to update only certain part of the doc rather then the whole thing.
One way you could reduce the amount of data in each doc could be to store separate entities in different collections, but without knowing how you access them and how often its hard to advise.
-
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