Mongodb-odm: Doctrine aggregation $project query is not working properly

Created on 11 Mar 2019  路  2Comments  路  Source: doctrine/mongodb-odm

I have combine the two columns into single columns for this using below one.

db.test.aggregate([{ $project: {category: ['$category1', '$category2'] } } ]).pretty();

It return perfect results in terminal, but doctrine it is not working?

My doctrine query:

$builder->project()->includeFields(['category'])->excludeIdField(['_id']);

It returns results as empty array of values.

Expecting Results as :


{
    "_id" : ObjectId("5c7cf5b4362a3134068b633d"),
    "category" : [
        "test1",
        "test2"
    ]
}
{
    "_id" : ObjectId("5c7cf5b4362a3134068b633e"),
    "category" : [
        "test1",
        "test4"
    ]
}
Question

Most helpful comment

Those two $project stages are completely different, as you can see when evaluating the result of $builder->getPipeline(). The ODM example you posted evaluates to this stage:

{
    $project: { 
        _id: false,
        category: true
    }
}

If you were to reproduce the $project stage from your MongoDB shell example, it would look like this:

$builder->project()
    ->field('category')
    ->expression(['$category1', '$category2']);

All 2 comments

Those two $project stages are completely different, as you can see when evaluating the result of $builder->getPipeline(). The ODM example you posted evaluates to this stage:

{
    $project: { 
        _id: false,
        category: true
    }
}

If you were to reproduce the $project stage from your MongoDB shell example, it would look like this:

$builder->project()
    ->field('category')
    ->expression(['$category1', '$category2']);

@alcaeus Perfect...It is works...Thanks for time.

Was this page helpful?
0 / 5 - 0 ratings