So I have Posts collection where each doc has createdAt and submitCost fields.
I would like to sort by createdAt first and then, by submitCostINSIDE EACH DAY.
Say if I have the following docs:
However, your createdAt needs to be a Date field (or a yymmdd string).
Also, you would be well advised to index that combination for performance if it’s important (run regularly and/or a large number of documents). In your server-side code:
But now, that I think of it - my createdAt field is already a Date field (I think, I oversimplified my example, argh…), thus they don’t represent any particular day, but a timestamp… so how could I possibly aggregate all the timestamps for a particular date and then make a sort inside of it?
I could, but my “fairly small” was somewhere in the 100s. Sorting larger sets is certainly doable, but it will tie up the event loop and may be better in a separate process. Or, use the aggregation pipeline - it’s what it was designed for.
If you still want some code I’ll post something later (about to go into a meeting).
So this is the sort of thing I mean. It doesn’t use the aggregation pipeline. It only needs enough memory to hold one day’s worth of results at a time. It doesn’t address the issue of what you do with the results (which are console.logged as each day is processed). My thinking is that you could roll this up into a publication and use the pub/sub API to deliver one day at a time to the client. You could also build a complete result array for all days - but then there wouldn’t be much point doing it this way in the first place!
I tried to implement a sort (a time sort) , but Meteor seems to ignore the second sort. It will only sort the first value and ignore the second one. It seems that when sorting dates, that it takes the time (hours, etc) into account and therefore ignores the second sorted value.
Also, what’s the best implementation of this kind of sort. You said to use the aggregation pipeline, but then you made a suggestion without the pipeline.
If your sort is based on a field which corresponds to a new Date(), then it sorts to the ms. So, it may be that the second part of your sort has no effect (it won’t be ignored). When you say you’re trying to do a “time sort”, what exactly do you want to achieve?
Well, it really depends on what you’re trying to do and the size/complexity of your data. The aggregation pipeline is really good for complex operations involving large numbers of documents and does a lot more than just sorting. Standard sort options on the find are good for simple data structures and can also be used with minimongo on the client (not true for the aggregation pipeline).
I’m trying to sort my objects exactly as the first person indicated. Except, the field for the date does correspond to new Date() and when I do as you suggest above it doesn’t properly sort them. I’m guessing that it has to do with the sort function not being aware that I want it to be sorted for each day (as above).