How do I give a list dynamically without hardcoding to MongoDB's $in?

Viewed 115

I have a list of ids that I want to search in the mongoDB collection.

Below code works fine, but there are multiple DB calls for each id and I cannot pass an array list without hardcoding.

$db->$collection->find( {
"_id" => { 
    '$in' => [ MongoDB::OID->new( value => "56de679ce64cfa37a61b94f3" ), 
               MongoDB::OID->new( value => "56d533dee64cfa2a970b2631" )
             ]
         } 
} );

But I want to pass the ids dynamically, something like this

$db->$collection->find( {
    "_id" => { 
               '$in' => [  @list   ]
             } 
 } );

Where @list contains two elements:

[
  '56de679ce64cfa37a61b94f3',
  '56d533dee64cfa2a970b2631'
]

How can I make this work by passing a dynamic array of ids?

1 Answers
Related