Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersecting Mongoid "in"-Queries

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?

like image 339
Phil Avatar asked May 13 '13 12:05

Phil


1 Answers

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

like image 170
Vishnuprasad R Avatar answered Sep 21 '22 11:09

Vishnuprasad R