On Storage Service, which is better: Searching Docs by "Key," or Storing Docs with Variable Numbers of "Keys?"

0 votes

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 :)


asked Apr 28, 2014 in App42 Cloud API-BaaS by David Harrison Turpi (23 points)

1 Answer

+1 vote

Hi David,

NoSQL storage service is optimized for any type of search either using key/vale or applying complex query on it.

You can have a collection which will stare userId and associated game inside it. For example JSON could be like this {'userId':'David','GameName':'MyGame'}. You can keep on storing this JSON for a user or for set of user as they start associating with the game. If you want to find out that how many game user x is associated you can do it using search operation.(Either by query or by key value)

Similarly you can store turn Ids in separate collection and can store information in given JSON format for example {'userId':'David','GameName','TurnId':'567'}

If you want to check turn of a user for given game, you can just do it using query method to find out the same.

Let me know if it makes sense.

answered Apr 29, 2014 by ajay123 (899 points)
Thanks, this really helps. Do you happen to know if searching by DocId is significantly faster than searching by query?
Yes, Search on docId is faster than query
Ok, got it. Is there a cap on a JSON document's size? I'm considering eliminating the Turns Collection and just nesting all the data in the documents of the Games collection. This will mean that the documents will have extremely variable size and may get very big. Is this considered bad practice when using the Storage Service?
Logically, singly JSON doc is row in a table and collection is like table. It would be great if you can create a separate collection and dump your JSON doc in it instead of putting it in single JSON. Putting everything is single JSON is not considered to be good practice.
Ok, that's what I figured. The problem is that I have a lot of operations that fire simultaneously. So, I have to make multiple API calls instead of just one, which can often be slow and it puts the burden on the client side to make sure everything is synched properly with the database. I'm using Cocos2d-x, so I don't have the option to use Custom server-side code as far as I can tell.
we will quickly add custom code in cocos2d-x SDK and update you on this.
Oh, that would be much appreciated, thank you! We have two other games in development and we'd love to use App42, but we'll likely need this (as well as other services, I imagine).
Hello David,
Apologize for the late reply.
You can download our latest SDK from the below link, which contains more features/services like custom code,session management service & buddy management  etc with some bug fixes.

Kindly download the latest SDK & Let us know if it helps.
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com