0

Good day. I have the following documents.

{
  id: 1,
  status: "sleeping"
}
{
  id: 2,
  status: "eating"
},
{
  id: 3,
  status: "drinking"
},
{
  id: 4,
  status: "drinking"
},
{
  id: 5,
  status: "sleeping"
},
{
  id: 6,
  status: "studying"
},
...

How do I sort them by status with the following order? sleeping, drinking, eating, studying

I'm using Angular-Meteor. And I'm trying to publish a collection with a limit in the server since I'm dealing with a very large data set.

I was looking into aggregate which I think is the right solution for this case but Meteor doesn't support it.

3 Answers3

1

Well, I ended up with attaching a number to sort the the documents in the following order: sleeping, drinking, eating, studying

{
  id: 1,
  status: "sleeping",
  statusOrder: 0
}
{
  id: 2,
  status: "eating",
  statusOrder: 2
},
{
  id: 3,
  status: "drinking",
  statusOrder: 1
},
{
  id: 4,
  status: "drinking",
  statusOrder: 1
},
{
  id: 5,
  status: "sleeping",
  statusOrder: 0
},
{
  id: 6,
  status: "studying",
  statusOrder: 3
},
...

It was easier, quite frankly.

But if you are interested with using MongoDB group for your app, I found something that might help. Link here.

Community
  • 1
  • 1
0

MongoDb just only support sort following A -> Z, Z -> A, 0 -> 9, 9 -> 0

Specify in the sort parameter the field or fields to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively.

Example: db.orders.find().sort( { "status": -1 } )

Phong Nguyen
  • 173
  • 3
  • 15
  • Ahhh. That's too bad. But is there any other way than using sort? I'm currently looking into aggregation but I don't think that's it either. Thanks for the quick answer. – Jonacius Villamor Jul 08 '16 at 01:56
  • It may be help you for aggregation http://www.mkyong.com/mongodb/mongodb-aggregate-and-group-example/ – Phong Nguyen Jul 08 '16 at 02:14
  • Thanks. I also found something that's close to what I need: http://stackoverflow.com/questions/22192098/mongo-how-to-sort-by-external-weight. Unfortunately, I'm using Angular-Meteor and it doesn't support reactive aggregation which my app needs. – Jonacius Villamor Jul 08 '16 at 03:03
0

The mongodb aggregation framework is available on the server side via the monbro:mongodb-mapreduce-aggregation package (for example). You can use this in your publication.

Another approach is to denormalize your data somewhat and include the sort key in each document. That involves a typical storage vs. speed tradeoff.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • Thanks for the reply! I ended up including sort keys for my documents. – Jonacius Villamor Jul 08 '16 at 06:02
  • Adding an artificial sort key should be beneficial for what reason exactly? The key is present, and the order is known. Imho, even grouping is not really necessary and this use case can be fulfilled 4 async limited requests for each of the keys. – Markus W Mahlberg Jul 08 '16 at 11:25
  • One could run a benchmark to see whether aggregation or the extra sort key were faster, as a function of the size of the data set. He wants to sort and limit, then publish, presumably reactively. – Michel Floyd Jul 08 '16 at 16:40