这是mongodb集合的聚合代码:
$mongo_db_name = "db_".$tenant_id;
$con = new MongoClient($this->config->item('mongo_instance'));
$db = $con->$mongo_db_name;
$collection = $db->$module;
$options = array('cursor' => array("batchSize" => 4 ));
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
$data = $collection->aggregate($pipeline,$options);
var_dump($data);
我使用上面的代码
收到此错误array(size = 4)'ok'=> float 0'errmsg'=> string'的每个元素 'pipeline'数组必须是一个对象'(length = 54)'code'=> int 14 'codeName'=>字符串'TypeMismatch'(长度= 12)
如果我尝试使用json_encode对管道进行编码,则结果为“null”。
如果我使用find(array('deleted'=>'0'));
,它会按预期返回所有文档。
任何人都可以帮助我解决我的错误或者我必须解决的问题吗?
答案 0 :(得分:0)
聚合管道采用数组中的对象,因此您的管道应该像
[
{$match:{}};
{$group:{}};
{$somepipelinestageOperator:{}}
]
但是如果你有一个带有数组作为标签而不是对象的管道,你会得到上面的错误(管道的每个元素都必须是一个数组..)。
[
[$match:{}],
[$group:{}],
[$somepipelinestageOperator:{}]
]
在您的代码中有用于匹配和组的数组,因此它失败了。
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
您可以打印您的管道,并看到您的管道阶段具有数组而不是对象。