MM queries still default to {$in: <value>} What if I don't want this?

23 views
Skip to first unread message

Jamie Orchard-Hays

unread,
Oct 16, 2014, 4:51:22 PM10/16/14
to mongo...@googlegroups.com
MongoMapper's queries default to using $in for queries when an Array is passed in as the value. For example:

where = {}
where[:high_haz_endpoints] = [params[:endpoint_type_id]] if params[:endpoint_type_id]
MaterialNameCasrn.where(where).paginate(query)

This generates:

"query"=>{:high_haz_endpoints=>{:$in=>["hum_tox_acute_dermal_irrit_score"]}}

Which is NOT what I want. Unfortunately, this shortcut in syntax breaks querying when I want to restrict it to just a specific Array. I remember discussing this with John years ago and I do not remember if there is a simple work-around.

Anyone know how to make my query not convert to an $in => [array] query? I need to search for a specific Array.

Cheers,

Jamie

Jon Kern

unread,
Jan 22, 2015, 12:22:45 AM1/22/15
to mongo...@googlegroups.com
Banged my head for the past couple of hours, it’s late, bleary eyed. I cannot find if I am doing something that is not supported or not

On the outside chance… Should the following work?

Ruby 2.1.1 (yes, as far as I can tell)
Rails 4.2 (I see *only* 4.0.x listed <<== is this my problem)
Using mongo_mapper (0.13.0.beta2) from git://github.com/mongomapper/mongomapper.git (at v0.13.0.beta2)

Thanks in advance

Hate to think I need to switch to mongoid :-(

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups "MongoMapper" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Heald

unread,
Jan 22, 2015, 1:17:31 AM1/22/15
to mongo...@googlegroups.com
It looks like this is explicitly expected behavior from Plucky:


You can hack around it, though it's a little nasty:

    Page.where(dm: ["x", "y"]).tap {|q| q.criteria.source[:dm] = ["x", "y"] }.first

Suggestions on what the syntax should be to say "search for this array exactly" as opposed to "search for any of these values"?

Jon Kern

unread,
Jan 22, 2015, 7:40:06 AM1/22/15
to mongo...@googlegroups.com
Thanks for the quick response, Chris…

When I run a command like this :
jon = User.create(first_name: 'jon', last_name: 'kern', email: 'jo...@example.com', organization: umfc)

It errors like this (whether or not I actually use validations in the model!):
NoMethodError: undefined method `exists?' for nil:NilClass
/Users/jon/.rvm/gems/ruby-2.1.1@blazemark4/bundler/gems/mongomapper-9b87dee50780/lib/mongo_mapper/plugins/validations.rb:51:in `validate_each'

Which looks like this in the code:
if @klass.exists?(conditions)
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end

Jamie Orchard-Hays

unread,
Jan 22, 2015, 11:18:55 AM1/22/15
to mongo...@googlegroups.com
I'm not clear on the the problem. Are you just having general problems getting MM to work with Rails 4.2, or are you trying to get around the query problem I had in my post you replied to from October 2014?

I got around the query problem this way:

# If Using Mongo 2.6
where[:high_haz_endpoints] = {:$eq => [params[:endpoint_type_id]]} # Mongo 2.6.x only

# If Using Mongo <= 2.4
where[:$and] = [{high_haz_endpoints: [params[:endpoint_type_id]]}, {high_haz_endpoints: {:$size => 1}}]

MaterialNameCasrn.where(where).paginate(query)

The convenience of having a query do $in automagically is not worth the pain it causes when you don't realize that's what's going on. Then you have to figure out how to get around it. Mongo 2.6 makes it a little easier.

I'd argue for making a breaking change in backwards compatibility on this one. It really sucks. The only other option I can think of would be to add some option that switches off the automatic $in. 

Jon Kern

unread,
Jan 23, 2015, 7:44:50 AM1/23/15
to mongo...@googlegroups.com
Sorry Jamie, I hope I didn't accidentally hijack some other thread?

My thread was about trying to upgrade an app from 4.0 to 4.2

$ mongod --version
db version v2.6.0

So in response to your question, I am just having general problems and wondered if MM works for Rails 4.2. If MM does not work with 4.2 then that’s my problem ;-)

If MM *does* generally work with Rails 4.2, then I will take another approach to my upgrade attempt.

I since reverted the app back to 4.0.x so that everything is back to working again...

Jamie Orchard-Hays

unread,
Jan 23, 2015, 11:22:36 AM1/23/15
to mongo...@googlegroups.com
I can't answer that Q. I've got Rails 3.2 and 4.0 apps running with MM. 

For both, I'm using:
gem 'mongo_mapper', :git => 'https://github.com/jnunemaker/mongomapper.git'
                    :ref => '9b87dee507802a8cb96f2d316802c9fd241448c8'

             :ref => "c271e9a546450feebc8e36e0aef0a32b3b084dff",
             :require => "joint"

I must have had some problems with later versions. I don't recall now. I haven't tried bumping up with the newer Rails 4 app.

Jamie

Jon Kern

unread,
Jan 23, 2015, 1:04:49 PM1/23/15
to mongo...@googlegroups.com
Thanks, Jamie!

Jon Kern

Reply all
Reply to author
Forward
0 new messages