Sign In Register

How can we help you today?

Start a new topic

What is the propper way to design your data type structure

I have a game with a bunch of unique characters. I have created their definitions on a meta collection containing all the general data of the characters like name, stats abilities and so on. Now for each player they will have a set of data representing their progress and personal options like this characters exp, rep, weapons and so on.

The Data Types system is not very transparent and i have no/little idea of what they made it for. I envisioned that i would have for each player one document per character containing all this progress data, and i would be able to query on the combination of a playerID and the character shortCode. I used to use the players id as the documentId in this dataType, but then i can only have one document. Since i cant autogenerate an Id and index an "owner" field in the document is it advised to just concatinate the playerId with a given shortCode and use that as the documentId? (example "f684sd6f4sd6f84asdf_CHARACTERNAME")  

You don't need to generate any id. MongoDB does it automatically. What you should do is save the PlayerId and CharacterId in your document.

{"PlayerId":"dw31fw6fw6f0", "CharacterId": "ddqd65wq1d6" }

When you want to retrieve the list of characters for a player, you just do a find({"PlayerId":"dw31fw6fw6f0"})

If you want a specific character you do findOne({"PlayerId":"dw31fw6fw6f0","CharacterId": "ddqd65wq1d6"})

You should also declare indexes like so

ensureIndex({ "PlayerId": 1 }); //index for getting all characters of one player

ensureIndex({ "PlayerId": 1, "CharacterId": 1 }); //index for getting a specific character of a player

This should be placed in a script you execute only once on your environment - not everytime you add/modify the collection.

Thanks alot for the fast reply Thorvald.

Thats exactly how i wanted to structure it, but the whole data type system i so confusing. When inserting from the data explorer it is required to enter an id, does the autogenerate id trigger only when you insert something from cloudCode?

Got a feeling your working on the Runtime mongo database Thorvald, wish i had the same option. The documentation and datatype system is just terrible, hope they prioritize documentation soon, as their service otherwise seems great. 

Managed to find a UUID generator function that anyone can use in their cloud code.

function generateUUID() { var d = new Date().getTime(); var uuid = 'xxxx-xxxx-4xxx-yxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x3|0x8)).toString(16); }); return uuid; };

This should be unique and generate an id simalar to this "847b-48b1-4789-bda3"

Login to post a comment