Retrieving data from collection, sorting a nested array of objects in reverse order


I have a Nodes collection that looks like:

I want to retrieve that collection and have the data field’s array sorted in reverse order or by outputTime ascending. Then, I want to have just the first 10 results (got this to work with $slice).

I’ve looked on the web to sort it, but modifiers like $position didn’t work.

Could I achieve this with Meteor and the collection system?



Basically, yes. Although, depending on the expected maximum number of entries you expect for each document, you may be better not using a nested object, but a separate collection for “data”.



I just fixed my problem by doing so. I stored the nested array in a seperate collection. Now I’m doing method calls on the client which find data with a sort and limit. (Those two aren’t supported in the client, right?)


They are supported on the client, but I always try to push as much heavy lifting onto the database engine, so (especially for big collections) I would do this server side. You could, however, just use Meteor’s pub/sub (rather than methods) - publish the sorted, limited collection to the client, being sure to use the same sort and limit on the client-side Collection.find().

Read the Meteor Guide article on publications for more information.