Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Filter with aggregation query builder #1669

Closed
fahimmahmoodmir opened this issue Nov 3, 2017 · 1 comment
Closed

Using Filter with aggregation query builder #1669

fahimmahmoodmir opened this issue Nov 3, 2017 · 1 comment

Comments

@fahimmahmoodmir
Copy link

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:

{ 
    "_id" : ObjectId("59f889e46803fa3713454b5d"), 
    "projectName" : "usecase-updated", 
    "classes" : [
        {
            "_id" : ObjectId("59f9d7776803faea30b895dd"), 
            "className" : "OLA"
        }, 
        {
            "_id" : ObjectId("59f9d8ad6803fa4012b895df"), 
            "className" : "HELP"
        }, 
        {
            "_id" : ObjectId("59f9d9086803fa4112b895de"), 
            "className" : "DOC"
        }, 
        {
            "_id" : ObjectId("59f9d9186803fa4212b895de"), 
            "className" : "INVOC"
        }
    ]
}

Now 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:

$qb = $dm->createQueryBuilder('Documents\Project');
$projectObj = $qb
    ->field('id')->equals("59f889e46803fa3713454b5d")
    ->field('classes')->elemMatch(
        $qb->expr()->field("id")->equals(new \MongoId("59f9d7776803faea30b895dd"))
    )
    ->hydrate(false)
    ->getQuery()
    ->getSingleResult();

First 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:

{ 
   "_id" : ObjectId("59f889e46803fa3713454b5d"), 
    "projectName" : "usecase-updated", 
    "classes" : [
          {
            "_id" : ObjectId("59f9d7776803faea30b895dd"), 
            "className" : "OLA"
          }
     ]
}

But 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:

        $qb = $dm->createAggregationBuilder('Documents\Project');
        $projectObj = $qb
            ->match()
            ->field('id')->equals($projectId)
            ->field('classes._id')->equals(new \MongoId($classId))
            ->project()
            ->filter('classes', "class", array('class._id' => new \MongoId($classId)))
            ->execute();

But i am getting following exceptions:

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):

I also tried adding a question on stackoverflow 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.

@malarzm
Copy link
Member

malarzm commented Dec 5, 2017

I see that SO question was answered so I'm closing this issue. If there's anything ODM could improve, please re-open :)

@malarzm malarzm closed this as completed Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants