{"action":"closed","issue":{"url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1669","repository_url":"https://api.github.com/repos/doctrine/mongodb-odm","labels_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1669/labels{/name}","comments_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1669/comments","events_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1669/events","html_url":"https://github.com/doctrine/mongodb-odm/issues/1669","id":270945957,"number":1669,"title":"Using Filter with aggregation query builder","user":{"login":"fahimmahmoodmir","id":2732245,"avatar_url":"https://avatars0.githubusercontent.com/u/2732245?v=4","gravatar_id":"","url":"https://api.github.com/users/fahimmahmoodmir","html_url":"https://github.com/fahimmahmoodmir","followers_url":"https://api.github.com/users/fahimmahmoodmir/followers","following_url":"https://api.github.com/users/fahimmahmoodmir/following{/other_user}","gists_url":"https://api.github.com/users/fahimmahmoodmir/gists{/gist_id}","starred_url":"https://api.github.com/users/fahimmahmoodmir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fahimmahmoodmir/subscriptions","organizations_url":"https://api.github.com/users/fahimmahmoodmir/orgs","repos_url":"https://api.github.com/users/fahimmahmoodmir/repos","events_url":"https://api.github.com/users/fahimmahmoodmir/events{/privacy}","received_events_url":"https://api.github.com/users/fahimmahmoodmir/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-11-03T11:19:39Z","updated_at":"2017-12-05T19:28:54Z","closed_at":"2017-12-05T19:28:54Z","author_association":"NONE","body":"Hi i am relatively new to ODM concepts with doctrine. I found there is no documentation updated for '$filter' array aggrgation filters. I am having trouble in returning only the matched embedded document using MongoDB ODM query builder in PHP. Each embedded document has a MongoID generated at the time of creation. Following is my document structure of collection Project:\r\n\r\n```\r\n{ \r\n    \"_id\" : ObjectId(\"59f889e46803fa3713454b5d\"), \r\n    \"projectName\" : \"usecase-updated\", \r\n    \"classes\" : [\r\n        {\r\n            \"_id\" : ObjectId(\"59f9d7776803faea30b895dd\"), \r\n            \"className\" : \"OLA\"\r\n        }, \r\n        {\r\n            \"_id\" : ObjectId(\"59f9d8ad6803fa4012b895df\"), \r\n            \"className\" : \"HELP\"\r\n        }, \r\n        {\r\n            \"_id\" : ObjectId(\"59f9d9086803fa4112b895de\"), \r\n            \"className\" : \"DOC\"\r\n        }, \r\n        {\r\n            \"_id\" : ObjectId(\"59f9d9186803fa4212b895de\"), \r\n            \"className\" : \"INVOC\"\r\n        }\r\n    ]\r\n}\r\n```\r\nNow i want to retrieve from the database only the class from the classes embedded documents which meets my criteria (i.e. class with a specific id).This is how i am building the query with simple query builder:\r\n\r\n```\r\n$qb = $dm->createQueryBuilder('Documents\\Project');\r\n$projectObj = $qb\r\n    ->field('id')->equals(\"59f889e46803fa3713454b5d\")\r\n    ->field('classes')->elemMatch(\r\n        $qb->expr()->field(\"id\")->equals(new \\MongoId(\"59f9d7776803faea30b895dd\"))\r\n    )\r\n    ->hydrate(false)\r\n    ->getQuery()\r\n    ->getSingleResult();\r\n```\r\n\r\nFirst i match with the project id then i match with the embedded document class id. I was expecting it to return only the embedded document of OLA like this:\r\n\r\n\r\n```\r\n{ \r\n   \"_id\" : ObjectId(\"59f889e46803fa3713454b5d\"), \r\n    \"projectName\" : \"usecase-updated\", \r\n    \"classes\" : [\r\n          {\r\n            \"_id\" : ObjectId(\"59f9d7776803faea30b895dd\"), \r\n            \"className\" : \"OLA\"\r\n          }\r\n     ]\r\n}\r\n```\r\n\r\nBut doctrine is returning the whole Project record (shown in the start of question).I also tried with aggregation query building with the $match aggregation and $filter array operators  of mongodb like this:\r\n\r\n```\r\n        $qb = $dm->createAggregationBuilder('Documents\\Project');\r\n        $projectObj = $qb\r\n            ->match()\r\n            ->field('id')->equals($projectId)\r\n            ->field('classes._id')->equals(new \\MongoId($classId))\r\n            ->project()\r\n            ->filter('classes', \"class\", array('class._id' => new \\MongoId($classId)))\r\n            ->execute();\r\n\r\n```\r\n\r\nBut i am getting following exceptions:\r\n\r\n`MongoResultException\\nCode: 16410\\nMessage: localhost:27017: FieldPath field names may not start with '$'.\\nFile: /var/www/html/doctor/lib/composer/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/CommandCursor.php\\nLine: 194\\nTrace: #0 /var/www/html/doctor/lib/composer/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/CommandCursor.php(194): MongoCommandCursor->rewind()\\n#1 /var/www/html/doctor/lib/composer/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/CommandCursor.php(257): Doctrine\\\\MongoDB\\\\CommandCursor->Doctrine\\\\MongoDB\\\\{closure}()\\n#2 /var/www/html/doctor/lib/composer/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/CommandCursor.php(195): Doctrine\\\\MongoDB\\\\CommandCursor->retry(Object(Closure))\\n#3 /var/www/html/doctor/lib/composer/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/CommandCursor.php(191): Doctrine\\\\MongoDB\\\\CommandCursor->rewind()\\n#4 /var/www/html/doctor/api/index.php(559):`\r\n\r\n\r\nI also tried adding a question on [stackoverflow](https://stackoverflow.com/questions/47059462/return-only-matched-embedded-document-using-doctrine-mongodb-odm-query-builder?noredirect=1#comment81068636_47059462) but not got any reply so far. Can anyone help me because i think i am using the wrong syntax for creating aggregation query builder but i am unable to find any example how to make it right."}}
{"actor":{"display_login":"malarzm"}}