Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reordering nodes can cause #ancestor to give results out of order #39

Closed
wants to merge 1 commit into from

Conversation

benhutton
Copy link

Here's some sample output.

This is the order that my parent ids are stored in the array:

irb(main):018:0> page.parent_ids
=> ["515c547c392d4ecdf6000004", "515c5732392d4ecdf600003a", "51d1f63fd268d9289600037c", "515c5740392d4ecdf60000f1"]

This is the order that they are returned by #ancestors:

irb(main):004:0> page.ancestors.map(&:id)
=> ["515c547c392d4ecdf6000004", "515c5732392d4ecdf600003a", "515c5740392d4ecdf60000f1", "51d1f63fd268d9289600037c"]

You'll notice the last 2 are flipped.

It seems that when no sort order is specified, MongoDB returns documents based on the order that they are stored in the disk: http://docs.mongodb.org/manual/reference/glossary/#term-natural-order

It seems to me that the least computationally expensive way to do this is to add a depth field, which may come in handy other places as well. I'm not very experienced with mongodb, so if anyone has a better suggestion I'm very open to it.

@benedikt
Copy link
Owner

benedikt commented Jul 5, 2013

Hi Ben,

thanks for pointing this out and your pull request! I'll look into this more in depth over the weekend. :-)

Cheers,

Benedikt

@benedikt
Copy link
Owner

benedikt commented Jul 8, 2013

Hi Ben,

thought about this over the weekend. What about ordering by parent_ids? As every ancestor gets one more parent_id in the parent_ids array this should output all parent ids in the correct order, right?

Bests,

Benedikt

@benhutton
Copy link
Author

This was my first thought, and would be the ideal way to implement it. parent_ids array order is the canonical ordering as far as I'm aware.

But like I said, my mongodb skills are too limited to know how to actually do this in a mongodb call, and it seemed like it was important to keep the call to #ancestors returning a mongoid criterion.

If you can point me in the right direction, I can make the changes.

@hems
Copy link

hems commented Jul 12, 2013

👍
nice stuff guys

@benedikt benedikt closed this in ae78bc2 Jul 13, 2013
@benhutton
Copy link
Author

Awesome, thanks @benedikt !

One question: do you also need to apply your fix to the Ordering module here: https://github.com/benedikt/mongoid-tree/blob/master/lib/mongoid/tree/ordering.rb#L49-L51 ?

@benedikt
Copy link
Owner

Uh oh… no… I missed that :) Thanks for pointing out! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants