Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb with rails, find by id in array

I can fetch an element by BSON id from Mongodb with

db.my_collection.find({_id: ObjectId("567bc95ab62c732243123450")})

And it works. But how can I get an array of ids? something like

db.my_collection.find({_id: [ObjectId("567bc95ab62c732243123450"])})

I tried different ways, as suggested on mongodb's website, but the interactive shell complained for syntax.

EDIT:

Found a problem:

it should be

db.my_collections.find({_id: { $in : [ObjectId("567bc95ab62c732243123450")]}})
like image 425
valk Avatar asked Nov 18 '12 10:11

valk


1 Answers

And in Rails:

MyCollection.find({'_id' => { "$in" => collection_ids}})
like image 122
valk Avatar answered Oct 01 '22 13:10

valk