I’m using DoctrineMongoDBBundle and want to get some records. In my repository class I have code:
return $this->createQueryBuilder()->field('categories.data')
->equals($categoryId)->getQuery()->execute();
In profiler’s log it generates absolutely working query:
db.recipe.find({ "categories.data": 16 }).sort([ ]);
When I run this query via console client it returns me record. But repository class get’s nothing.
My server’s log looks like this:
query test_database.recipe query: { $query: { categories.data: "2" }, $orderby: {} } ntoreturn:0 keyUpdates:0 nreturned:0 reslen:20 0ms
But if i run plain PHP script to get same data:
<?php
$mongo = new Mongo();
$col = $mongo->test_database->recipe;
foreach ($col->find(array("categories.data" => 2)) as $r) {
print_r($r);
}
I got result and server’s log looks like this:
query test_database.recipe query: { categories.data: 2 } ntoreturn:0 keyUpdates:0 nreturned:1 reslen:1037 0ms
so difference is:
query test_database.recipe query: { $query: { categories.data: "2" }, $orderby: {} } ntoreturn:0 keyUpdates:0 nreturned:0 reslen:20 0ms
query test_database.recipe query: { categories.data: 2 } ntoreturn:0 keyUpdates:0 nreturned:1 reslen:1037 0ms[/quote]
Checked both MongoDB 2.1.1 and 1.8.1, deps file:
[symfony]
git=http://github.com/symfony/symfony.git
version=origin/2.0
[doctrine-common]
git=http://github.com/doctrine/common.git
version=2.2.2
[doctrine-dbal]
git=http://github.com/doctrine/dbal.git
version=2.1.7
[doctrine]
git=http://github.com/doctrine/doctrine2.git
version=2.1.7
[doctrine-mongodb-odm]
git=http://github.com/doctrine/mongodb-odm.git
[DoctrineMongoDBBundle]
git=http://github.com/doctrine/DoctrineMongoDBBundle.git
target=/bundles/Symfony/Bundle/DoctrineMongoDBBundle
version=origin/2.0
Based on this log you shared:
You’re matching by the string
"2"in the first query, but the integer2in the second. These are not equivalent in MongoDB. Consider:Have you tried casting
$categoryIdto an integer before using it in the query builder?