Find documents including element in Array field with mongomapper?

我是研究僧i 提交于 2019-12-10 06:10:16

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/8297553/find-documents-including-element-in-array-field-with-mongomapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!