Search code examples
phpmongodbmongodb-queryaggregation-frameworkphp-mongodb

How to convert below MongoDB query into PHP?


Below query worked fine in Studio 3T and Robomongo, But I want to convert it into PHP format,

Following is my query,

db.news.aggregate([
{
      $match: {  "_id" : "1" }
},
{
    "$project": {
        "comments": {
            "$filter": { 
                "input": "$comments",
                "as": "comment", 
                "cond": { "$eq": [ "$$comment.status", "active" ] } 
            }
        } 
    }
}, {
    "$project": {
        "comments": {
            "$slice": [
                {
                    "$slice": [
                        "$comments",
                        {
                            "$subtract": [ { "$size": [ "$comments" ] }, 1 ]
                        }
                    ]
                }, -1
            ]
        }
    }
}])

I have tried below, But it giving error "Error: localhost:27017: FieldPath field names may not be empty strings."

PHP converted sample:

<?php
        $commentsAggregate=array(
                            array('$match' => array("_id" => "1")),
                            array('$project' => array(
                                    "comments" => array(
                                        '$filter' => array(
                                            "input" => "$comments",
                                            "as" => "comment",
                                            "cond" =>  array('$eq' => array( "$$comment.status", 'active'))
                                    )))),
                            array('$project' => array(
                                "comments" => array(
                                '$slice' => array(array(
                                        '$slice' => array("$comments",array('$subtract' => array( array( '$size' => array("$comments")),1)))
                                        ), -1)
                                )))
                        );
$cursor=$collectionNews->aggregate($commentsAggregate);

Please help me to convert above query.


Solution

  • The error message "FieldPath field names may not be empty strings" originates from the server. Looking at the example PHP code you've provided, I notice that you're inconsistently using single- and double-quoted strings. In particular, these two strings stand out:

    • "$$comment.status"
    • "$comment"

    PHP is evaluating variable references inside double-quoted strings. Assuming the local scope does not actually have a $comment variable defined, those strings are going to resolve to "$.status" and "", respectively. As evidenced in this script and execution output on 3v4l.org, those examples should at least result in a PHP notice for an undefined variable (my local PHP configuration happens to report this at the "error" level). If you have no record of that error message, I would suggest the following:

    • Check your error_reporting configuration.
    • Ideally, you should report everything (E_ALL) in a development environment. Full reporting is also advisable for production, although there you would likely want to disable display_errors (for security) and instead ensure everything is logged properly.
    • If it turns out the error was logged, look into how it was missed while debugging this issue.

    As for fixing the root cause, you should be mindful to use single-quoted strings when writing MongoDB queries/commands in PHP. There is a note about this in the MongoCollection::find() documentation, but it's not something we repeat on every page, as PHP's double-quoted string evaluation is outside the control of the driver.