Searching for documents, including an element in the Array field using mongomapper?

I am new to mongodb / mongomapper and cannot find the answer to this question.

I have a mongomapper class with the following fields

key :author_id, Integer key :partecipant_ids, Array 

Say I have a β€œrecord” with the following attributes:

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

I want to get all the objects in which partecipant with identifier 15 is involved. I did not find a mention in the documentation.

It’s strange that I did this request before

 MessageThread.where :partecipant_ids => [15] 

which worked, but after a (possibly) change in the gem / mongodb version, it stops working. Unfortunately, I do not know which version of mongodb and mongomapper I used before.

+4
source share
1 answer

In current versions of MongoMapper, this will work:

 MessageThread.where(:partecipant_ids => 15) 

And that should work too ...

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

... because plucky autoexpands is:

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

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

I would say take a look at your data and try the queries in the Mongo console to make sure you have a working request. MongoDB queries translate directly to MM queries, with the exception of the above (and a few other minor) caveats. See http://www.mongodb.org/display/DOCS/Querying

+7
source

Source: https://habr.com/ru/post/1383440/


All Articles