4

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.

2
  • What version of rails/mongomapper/mongodb etc are you using now? Commented Nov 28, 2011 at 15:14
  • rails 3.0.10, mongo (1.5.0), mongo_mapper (0.10.1) Commented Nov 30, 2011 at 8:14

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

Also see the plucky tests, they are probably clearer to read than the source: github.com/jnunemaker/plucky/blob/master/test/plucky/…
strange fact. With MessageThread.where(:partecipant_ids => ['15']) it works. For some reason it's treated like an array of strings now? I am 100% sure that before the upgrade it was working with integers.
You can try adding :typecast => 'Integer' to your :partecipant_ids key. I'm not sure if that will break the many :in association.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.