Created
March 31, 2020 07:40
-
-
Save pierrejoye/a9f516819ebc5deaccdd4fdd7d93b72d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Works | |
db.getCollection('users').find( { | |
_id: { | |
$in: [ | |
ObjectId("5ba8633c5e01bb5453782a12"), | |
ObjectId("5ba8802b5e01bb25e335c352"), | |
ObjectId("5d71cb23ab360c12ba1f0df2"), | |
ObjectId("5dc0e12eab360c77e940b0c2"), | |
ObjectId("5c92e827ab360c2ee837dae2"), | |
ObjectId("5ba868e45e01bb5e9d51b6f3"), | |
"0050I000007caxBQAQ" | |
] | |
} | |
}) | |
Using only string fails: | |
db.getCollection('users').find( { | |
_id: { | |
$in: [ | |
"5ba8633c5e01bb5453782a12", | |
"5ba8802b5e01bb25e335c352", | |
"5d71cb23ab360c12ba1f0df2", | |
"5dc0e12eab360c77e940b0c2", | |
"5c92e827ab360c2ee837dae2", | |
"5ba868e45e01bb5e9d51b6f3", | |
"0050I000007caxBQAQ" | |
] | |
} | |
*/ | |
$res = $this->createQueryBuilder() | |
->field('id')->in($ids) | |
->getQuery() | |
->execute() | |
->toArray(); // Failed | |
$newIds = []; | |
foreach ($ids as $id) { | |
$newIds[$i] = strlen($id)<24 ? $id : new ObjectId($id); | |
} | |
return $this->createQueryBuilder() | |
->field('id')->in($newIds) | |
->getQuery() | |
->execute() | |
->toArray(); // works but it was not necessary last time we talked about it (strategy NONE for _id) | |
/* | |
"query" => array:1 [▼ | |
"_id" => array:1 [▼ | |
"$in" => array:7 [▼ | |
0 => "5c92e827ab360c2ee837dae2" | |
1 => "5ba8633c5e01bb5453782a12" | |
2 => "5ba8802b5e01bb25e335c352" | |
3 => "5d71cb23ab360c12ba1f0df2" | |
4 => "5dc0e12eab360c77e940b0c2" | |
5 => "5c92e827ab360c2ee837dae2" | |
6 => "5ba868e45e01bb5e9d51b6f3" | |
] | |
] | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment