how do you retrieve an array of IDs
in Mongoid
?
arr=["id1","id2"]
User.where(:id=>arr)
You can do this easily if you are retrieving another attribute
User.where(:nickname.in=>["kk","ll"])
But I am wondering how to do this in mongoid -> this should be a very simple and common operation
Or simply:
Remember that the ID is stored as
:_id
and not:id
. There is anid
helper method, but when you do queries, you should use:_id
:Often I find it useful to get a list of ids to do complex queries, so I do something like:
The solution above works fine when amount of users is small. But it will require a lot of memory while there are thousands of users.
will create a list of User objects with nil in each field except id.
The solution (for mongoid 2.5):
The above method suggested by browsersenior doesn't seem to work anymore, at least for me. What I do is: