9

According to the mongoid documentation on Explicit Merging ("Queryable#in - defaults to intersect") I would expect the following query:

Contact.in(id: ['a', 'b']).in(id: ['b', 'c'])

to result in something like this:

=> #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>["b"]}}
  options:  {}
  class:    Contact
  embedded: false>

But instead I get an overwrite for all imaginable cases:

[1] pry(main)> Contact.in(id: ['a', 'b']).in(id: ['b', 'c'])
=> #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>["b", "c"]}}
  options:  {}
  class:    Contact
  embedded: false>

[2] pry(main)> Contact.in(id: ['a', 'b']).intersect.in(id: ['b', 'c'])
=> #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>["b", "c"]}}
  options:  {}
  class:    Contact
  embedded: false>

[3] pry(main)> Contact.in(id: ['a', 'b']).union.in(id: ['b', 'c'])
=> #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>["b", "c"]}}
  options:  {}
  class:    Contact
  embedded: false>

Am I doing something wrong?

5
  • 2
    Interesting enough, if I use field name other than id_id or any random field name, it works properly. Using _id in your case may be a way to go, but it still looks like a bug.
    – Gosha A
    May 13, 2013 at 12:51
  • Actually, it would behave in this strange way not only with _id but with field aliases as well. I think I know how to fix that in Origin.
    – Gosha A
    May 13, 2013 at 13:17
  • That's the solution, thanks. It works using _id. Should I open an issue for the Origin project on github? Or since you know how to fix it, do you open a pull-request? Would be great!
    – Phil
    May 13, 2013 at 13:48
  • I just opened a PR fixing this — github.com/mongoid/origin/pull/83
    – Gosha A
    May 13, 2013 at 13:49
  • @Phil please answer your own question and mark it as resolved if you found the answer.
    – Jaffa
    Nov 30, 2015 at 8:50

1 Answer 1

1

The issue you are facing is caused by Mongoid gem. Upgrading to latest version of Mongoid gem will solve the issue.

More information regarding the bug can be found here

https://github.com/mongoid/origin/pull/83

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.