Jori
Jori

Reputation: 222

How to sort collection's nested array descending in meteor?

I have a Nodes collection that looks like: http://codepaste.net/kttsj1.

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

How could I achieve this?

Thanks,

Jori

Upvotes: 0

Views: 172

Answers (3)

Jori
Jori

Reputation: 222

I fixed my problem!

I simply gave the nested data array its own collection called SensorData and linked that to the nodes collection with an id.

Now in a Meteor method I can SensorData.find({nodeId: id}, {sort: {outputTime: -1}, limit 10});.

I have to do it in a method though, because it seems that client side does not support this query. (Probably the sort or limit thats not supported)

Thanks for the help to everyone who responded :)

Upvotes: 0

sravanthi
sravanthi

Reputation: 247

Try this:

If it is robomongo:

YourCollection.find({},{sort:{"data.outputTime":1}}).limit(10);

If it is JS:

YourCollection.find({},{sort:{"data.outputTime":1},limit:10});

Upvotes: 0

ickyrr
ickyrr

Reputation: 2123

I think you're looking for this:

YourCollection.find({},{sort:{name:-1}}); //for descending order

YourCollection.find({},{sort:{name:1}}); //for ascending order

and if you want to limit the number of results:

YourCollection.find({},{sort:{name:-1},{limit: 10}});

hope this helps.

Upvotes: 1

Related Questions