Today was frustrating. I was working on another query that would use the time line display component I made, and I wanted this query to display the actions a user performed (scrolling to the bottom, selecting text, etc) during a visit, and when they performed them. After figuring out the Mongo syntax to perform this query, which involved pulling out the "action" documents out of the "hit" documents, I came to the conclusion we needed to change the schema of the hits and actions in our database to make this more feasible. We were being a bit too minimalist, just storing the actions embedded inside the hits like so:

hit = {
    date: ...,
    scrollActions: [
        { ... },
        { ... },
    clickActions: [
        { ... },
        { ... },

The problem with this is that after performing the setUnion and unwind parts of our query, we lose the name of those actions. It becomes just "actions" and this means my display components, like my time line, won't know what type of action it is. That prevents me from displaying anything about the action. All of our actions have different schemas.

After discussing this with my team mate, we came to the conclusion we could get around this by having the query send everything over without manipulating it, and just using server-side JavaScript to parse what we need out of the results when the server gets it, but this isn't efficient long term for queries with large results, and we decided that a better schema would be better long term anyways. Our changes are going to include storing the action's type (as a String) as an attribute of the action. This is simpler than parsing results that don't have the action's type as an attribute, instead trying to parse the keys' values (and hoping that the keys didn't get mucked up as part of the Mongo query).

I won't be working on this again until Thursday, because I'll be attending a Polytechnics Canada event in Ottawa tomorrow, so my team mate will be making these schema changes before I resume work, probably to continue creating queries for the Get API.

Note: This was originally posted on the blog I used for my co-op term while at Seneca College ( before being imported here.