Best way of storing end of day stock price data in Mongo


#1

I am hoping that someone has done something similar to this and can give me a tip before I embark on trial and error.

I’m building a small app to keep track of my share portfolio.
I envision two collections, one to track my portfolio buys/sells etc…
The other collection to track each stock codes performance.

That way if two people have the same stock, in their subscription the server can limit the records to only publish those which matter. Eg, if user A has stock XXX, then the publication will only publish the XXX document and limit the data contained to be relevant dates (not the entire history).

This is how I am thinking of structuring the data:

  stockData
      |- _id
      |- stock code
      |- mostRecentData (updated regularly throughout the day)
            |- date
            |- open
            |- high
            |- low
            |- close
            |- volume
      |- historicData (only updated at end of day)
            |-[{}] - array of objects
                |- date
                |- open
                |- high
                |- low
                |- close
                |- volume

The issue that I foresee is that there will be very little observer reuse as every portfolio will have a different subscription. I am not sure how this might negatively impact server performance. This is where trial and error was going to come into play…

I had hoped that if I just dump the data to the client, I can graph and run some logic on the client side rather than doing this all server side… I had previously built a similar app in PHP and it was all done on the server.

I did have a look at Mongo’s documentation but the examples they gave did not seem to fit my use case.


#2

So I have begun my trial and error.
I decided not to use the date as a key to the array, as it appears hard to query on a key. (this was one idea I had)

At the moment I am struggling to work out how I can subscribe to a collection.

For example, User A has a portfolio with Stock XXX in it.
They bought those shares 1st of Jan 2015, and sold them again 1st of March 2015.

I want them to get a subscription like this:

stockData.find({
  code: 'XXX',
  'historicData.date': {
    $gt: 'Thu Jan 01 2015 00:00:00 GMT+1100',
    $lt: 'Sun Mar 01 2015 00:00:00 GMT+1100'
  }
});

The server will need to run this publish function for each of the stock codes, and that way the client should have a local copy of all the data and can do its own calculations…

It seems like this may not be the best way to handle this data…