如何将下面的MongoDB查询转换为PHP? [英] How to convert below MongoDB query into PHP?

查看:102
本文介绍了如何将下面的MongoDB查询转换为PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的查询在Studio 3T和Robomongo中运行良好,但是我想将其转换为PHP格式,

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

以下是我的查询

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
            ]
        }
    }
}])

我在下面尝试过,但是给出了错误"Error: localhost:27017: FieldPath field names may not be empty strings."

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

PHP转换后的示例:

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.

推荐答案

错误消息"FieldPath字段名称不能为空字符串"起源于

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"
  • "$$comment.status"
  • "$comment"

PHP正在对双引号字符串内的变量引用进行评估.假设本地范围实际上没有定义$comment变量,则这些字符串将分别解析为"$.status""".正如此脚本和3v4l.org上的执行输出所证明的那样,这些示例至少应 >导致未定义变量的PHP通知(我的本地PHP配置碰巧在错误"级别报告了此错误).如果您没有该错误消息的记录,则建议以下操作:

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:

  • 检查您的 error_reporting 配置.
  • 理想情况下,您应该报告开发环境中的所有内容(E_ALL).对于生产,也建议使用完整报告,尽管您可能希望禁用
  • 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.

至于解决根本原因,在用PHP编写MongoDB查询/命令时,请注意使用单引号引起来的字符串. MongoCollection::find() 文档中对此有一个注释,但不是我们在每个页面上都重复了一些内容,例如PHP的双引号字符串评估在驱动程序的控制范围之外.

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.

这篇关于如何将下面的MongoDB查询转换为PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆