To utilize the $in operator with the _id field in MongoDB, you must ensure that you provide an array of MongoId's, rather than an array of MongoId of arrays, as the latter is not supported.
Therefore, it's necessary to preprocess your array before executing the query or maintain these ids as MongoId's.
You can conveniently accomplish this preprocessing using PHP's array_map function or a foreach loop. Below are illustrative examples of both methods:
// Using array_map()
$mongoIds = array_map(function($id) {
return new MongoDB\BSON\ObjectId($id);
}, $array);
// Using foreach loop
$mongoIds = [];
foreach ($array as $id) {
$mongoIds[] = new MongoDB\BSON\ObjectId($id);
}
Once you have an array of MongoId's, you can proceed to construct your query using the $in operator:
$query = [
'_id' => [
'$in' => $mongoIds
]
];
Executing this query will retrieve all documents where the _id field matches any of the provided MongoId's.