1

In meteor let's say I have a minimongo collection with the following documents:

{ "_id" : 1, "item" : "abc1", description: "ball", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "shoe", qty: 100 }
{ "_id" : 3, "item" : "xyz1", description: "tops", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "glue", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "glue", qty: 180 }
{ "_id" : 6, "item" : "XXX1", description: "shoe", qty: 500 }

and I want to use the $or operator to return the documents that either have qty > 250 or the description is shoe.

result: { $or: [ { $gt: [ "$qty", 250 ] }, { $in: [ "$description", "shoe" ] } ] }

How can I make it so that the returned cursor is sorted in the following order:

  1. document(s) that have both qty > 250 and description matching shoe
  2. documents(s) that have just the description shoe
  3. documents(s) that have just qty > 250

Searching around it seems like a aggregations ref 1, ref 2 might be one option except for the fact that minimongo for meteor does not seems to appear to support aggregations at this point.

If this cannot be accomplished with a minimongo sort specifier/query etc, how would one alternatively sort the results as described above (assuming that it is done on the array/object returned from doing a fetch on the cursor)

Community
  • 1
  • 1
funkyeah
  • 3,074
  • 5
  • 28
  • 47
  • are you using custom publications and subscriptions? if so, you could create a publication for items with qty>250 and a separate publication for items that have description shoe. You can use multiple subscriptions for one collection and Meteor will combine the results. – Ramsay Lanier Feb 17 '15 at 17:27
  • Unfortunately, no I'm not. I'm using a search package that constructs the query and publication. It does seem though that with $or it might be impossible for minimongo to sort client side intelligently anyway. Multiple subscriptions and manually sorting might be the only option. – funkyeah Feb 18 '15 at 18:17

0 Answers0