Hi App42 Community,
I'm developing an asynchronous 2-player game where users can invite each other to a game. Each game consists of a variable number turns. Coming from the relational (SQL) world, I designed my collections like this:
[Collection 1: Games]
Each Document contains a Game, which contains some unique data, including the usernames involved.
[Collection 2: Turns]
Each Document contains a Turn, which contains some unique data, as well as a Document Id pointing to a Game in Collection 1.
When my game starts, it first searches through the documents in the Games collection (Collection 1) for usernames that match the username of the current user. Note that this is a search by key/value, i.e., within the JSON of each document. Upon hearing back from the server, I get the Document Ids of whatever Games this user is involved in.
Next, once it gets these Games, it searches the Turns collection (Collection 2) for any Turns that include a Document Id pointing to a Game in collection 1. This is again a search by key/value.
After doing some research into NoSQL, I became aware that I'm perhaps doing everything all wrong! Perhaps I should be avoiding searching documents by key/value (because, I assume it's slower for NoSQL than for SQL), instead opting for variable-length documents that contain however many Document Ids I need. Essentially, what I'm asking is, in NoSQL, is it better to rely on searching documents by keys, or is it better to store documents with variable numbers of keys?
To illustrate what I mean, let me propose a new, refactored version of the system I described above:
[Collection 0: Users]
Each document contains a list of active Games associated with a user. There could be 1 game, 0 games, or 100 games. It doesn't matter. Each document stores however many document Ids to the games table as this user has games, using JSON to organize them. Whenever we query the user, we get all of the game document ids and download them after one API call.
[Collection 1: Games]
Each game stores any number of Turn Ids. However many turns this Game has, there are turn ids for, organized using JSON.
[Collection 2: Turns]
Each turn stores whatever unique data is related to this turn.
So, my question to you, is which design, if either, should I go with? Which is the NoSQL Storage Service more optimized for? Bear in mind, I'm using Cocos2d-x, which has only very basic App42 functionality, as described in the documentation: http://api.shephertz.com/app42-dev/cocos2dx-backend-apis.php
Thanks in advance. Any help is much appreciated :)
David