Re: [MongoMapper] Sorting collections by arrays?

38 views
Skip to first unread message

Brian Hempel

unread,
Jun 22, 2012, 5:56:01 PM6/22/12
to mongo...@googlegroups.com
If your result set is small, you can sort it in Ruby.

The reason Mongo sorts that way is because of how its indexes are implemented. The indexes probably look like...

== my_docs.animal ==
armadillo -> doc2
cat -> doc5
dog -> doc5
zebra -> doc2

…w/no information about array position. Based on the docs, it looks like this is your only option for Array indexing…


…but the Mongo list might give you better advice.

Brian


On Jun 22, 2012, at 5:21 PM, Seth Boyles wrote:

I have a collection I want to sort by an array attribute.  However I want this sorted lexically by each element of the array.  For example, ["dog, "cat"] should be sorted in front of  ["zebra", "armadillo"] because the first element of the first array "dog" is head of the first element of the second array.  However this is not how mongomapper or mongodb does this--instead the ["z","a"] array comes first because the second element "armadillo" is before all the other elements in the first array--and I was wondering if anyone knew if mongo or mongomapper had any way of supporting this type of lexical sort.

Thanks,

Seth

--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Justin Dossey

unread,
Jun 22, 2012, 6:09:23 PM6/22/12
to mongo...@googlegroups.com
I would be tempted to implement a merge sort using map/reduce.  It might not meet your needs, but it would be fun to code up!

Justin Dossey

Jon Kern

unread,
Jun 25, 2012, 9:52:50 PM6/25/12
to mongo...@googlegroups.com
if your result set is not small, you can add a callback to update a
special column you can then use reliably for sorting...

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Brian Hempel said the following on 6/22/12 5:56 PM:
> If your result set is small, you can sort it in Ruby.
>
> The reason Mongo sorts that way is because of how its indexes are
> implemented. The indexes probably look like...
>
> == my_docs.animal ==
> armadillo -> doc2
> cat -> doc5
> dog -> doc5
> zebra -> doc2
>
> �w/no information about array position. Based on the docs, it looks
> like this is your only option for Array indexing�
>
> http://www.mongodb.org/display/DOCS/Multikeys
>
> �but the Mongo list might give you better advice.
Reply all
Reply to author
Forward
0 new messages