如何在MongoDB PHP中减去 [英] How to subtract in mongodb php

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

问题描述

 $getdataPipeline = array(
            array(
                '$match' => array(
                    'project_id' => array('$in' => $mysql_project_id) // Validating project ID
                ),
                '$match' => array('project_id' => $project_id)
            ),
            array(
                '$group' => array(
                    '_id' => array('pro_id' => '$project_id', 'uid' => '$user_id'),
                    "wh" => array('$subtract' => array(array('$sum' => '$toltal_timein_minutes'), array('$sum' => '$holding_durationin_minutes')))

                ))
        );

运行查询:

  $ValidProjectIdInMongo = $collection->aggregate($getdataPipeline);

我遇到类似的错误

带有消息'localhost:27017的未捕获异常'MongoResultException':异常:未知的组运算符'$ subtract'

Uncaught exception 'MongoResultException' with message 'localhost:27017: exception: unknown group operator '$subtract''

推荐答案

$sum 是要与 $group 一起使用的累加器,因此它必须是所使用的顶级运算符.因此,您的其他操作需要在$sum内进行:

$getdataPipeline = array(
    array(
        '$match' => array('project_id' => $project_id)
    ),
    array(
        '$group' => array(
            '_id' => array('pro_id' => '$project_id', 'uid' => '$user_id'),
            "wh" => array( 
                '$sum' => array( 
                    '$subtract' => array( 
                        '$toltal_timein_minutes', 
                        '$holding_durationin_minutes'
                    ) 
                ) 
            )
        )
    )
);

您可以在此处执行此操作,因为这是基本的减法,但是更复杂的操作通常需要在$group之后的一个单独的$project阶段.

You can do it here because this is basic subtraction, but more complex operations would generally require a separate $project stage after the $group.

还请注意,您$match管道阶段是不正确的,实际上将被解释,就像我在上面重新编写的那样.您可能是在两个条件下都表示$in的条件,这是合乎逻辑的$or.

Also note that yout $match pipeline stage is incorrect and will actually be interpretted just as I have re-written above. You perhaps mean in $in condition for both the possible values, which is a logical $or.

您还可能在字段名称中输入键入错误.

You also have what looks like typing errors in the field names.

这篇关于如何在MongoDB PHP中减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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