2

I'm using meteor and trying to sort a collection by a child

Example: sorting [{foo:{bar:1}},{foo:{bar:9}},{foo:{bar:3}}] by foo.bar.

I tried to do it with the following code:

Collection.find({},{sort:{"foo.bar":1}})

However, I keep getting the following error

error: { "$err" : "Unsupported projection option: foo.bar", "code" : 13097 }

Is there some alternate way I should go about sorting by child nodes? This is MiniMongo so I can't use aggregate.

Thanks.

Stennie
  • 63,885
  • 14
  • 149
  • 175
mjkaufer
  • 4,047
  • 5
  • 27
  • 55
  • 1
    From the error message it sounds like your sort option is being interpreted as a projection. In a normal MongoDB query you could do something like `db.foo.find({},{}).sort({"foo.bar":1})`, however note that sorting by child nodes will likely not produce the expected results if you have multiple matching children per document. – Stennie Aug 25 '14 at 06:11
  • @Stennie Minimongo does it differently http://docs.meteor.com/#find – Neil Lunn Aug 25 '14 at 06:15
  • 1
    @NeilLunn I know MiniMongo does `find()` differently, but the error message "Unsupported projection operator" implies the `sort` value is being incorrectly interpreted as a projection. I'm not sure if MiniMongo supports projection on subdocs, but presume someone will have a more definitive answer ;-) – Stennie Aug 25 '14 at 06:17
  • 1
    The stab here which was part of my basic correction is that this works for me. At least on recent MongoDB and meteor versions ( 2.6.4 amd 0.8.3 repectively ) this works with the insertion of three documents and sorting as shown. So maybe you are actually doing something different to what is presented here? – Neil Lunn Aug 25 '14 at 12:59
  • Really weird... I didn't even bother to try `.sort` because I thought, like you said, minimongo didn't support it. It does work. I remember at one point or another `.sort` didn't work but I guess that's changed. Thanks! – mjkaufer Aug 25 '14 at 13:06
  • So some weird stuff went down. I can use `.sort` in the meteor mongo console and I can't use the `{sort:...}` method, but the reverse applies when using it in the actual webpage. – mjkaufer Aug 25 '14 at 13:19

0 Answers0