Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find documents including element in Array field with mongomapper?

I am new to mongodb/mongomapper and can't find an answer to this.

I have a mongomapper class with the following fields

key :author_id, Integer
key :partecipant_ids, Array

Let's say I have a "record" with the following attributes:

{ :author_id => 10, :partecipant_ids => [10,15,201] }

I want to retrieve all the objects where the partecipant with id 15 is involved. I did not find any mention in the documentation.

The strange thing is that previously I was doing this query

MessageThread.where :partecipant_ids => [15]

which worked, but after (maybe) some change in the gem/mongodb version it stopped working. Unfortunately I don't know which version of mongodb and mongomapper I was using before.

like image 852
Stefano Lampis Avatar asked Feb 23 '23 07:02

Stefano Lampis


1 Answers

In the current versions of MongoMapper, this will work:

MessageThread.where(:partecipant_ids => 15)

And this should work as well...

MessageThread.where(:partecipant_ids => [15])

...because plucky autoexpands that to:

MessageThread.where(:partecipant_ids => { :$in => [15] })

(see https://github.com/jnunemaker/plucky/blob/master/lib/plucky/criteria_hash.rb#L121)

I'd say take a look at your data and try out queries in the Mongo console to make sure you have a working query. MongoDB queries translate directly to MM queries except for the above (and a few other minor) caveats. See http://www.mongodb.org/display/DOCS/Querying

like image 77
Brian Hempel Avatar answered Feb 25 '23 18:02

Brian Hempel