Php Mongo$addToSet数组
我有一个工作问题:Php Mongo$addToSet数组,php,mongodb,aggregation-framework,Php,Mongodb,Aggregation Framework,我有一个工作问题: $offenses = \App\LawCase::raw(function ($collection) { return $collection->aggregate([ [ '$match' => ['active' => true, 'type' => 'criminal', 'current_offense_literal'
$offenses = \App\LawCase::raw(function ($collection) {
return $collection->aggregate([
[
'$match' => ['active' => true, 'type' => 'criminal', 'current_offense_literal'=> ['$exists' => true]]
],
[
'$group' => ['_id' => '$current_offense_category', 'offense_literals' => ['$addToSet' => '$current_offense_literal']]
]
]);
});
我只想为上面的addToSet添加一个附加值。例如,每个当前的\u冒犯\u文字也有一个当前的\u冒犯\u文字\u值,我想将其与当前的\u冒犯\u文字一起添加
我尝试的是:
$offenses = \App\LawCase::raw(function ($collection) {
return $collection->aggregate([
[
'$match' => ['active' => true, 'type' => 'criminal', 'current_offense_literal'=> ['$exists' => true]]
],
[
'$group' => ['_id' => '$current_offense_category', 'offense_literals' => ['$addToSet' => ['$current_offense_literal'=>['$push'=> '$current_offense_literal_value']]]]
]
]);
});
但我得到了:
无法识别的表达式“$current\u offension\u literal”。尝试执行此操作
$offenses = \App\LawCase::raw(function ($collection) {
return $collection->aggregate([
[
'$match' => ['active' => true, 'type' => 'criminal', 'current_offense_literal'=> ['$exists' => true]]
],
[
'$group' => ['_id' => '$current_offense_category', 'offense_literals' => ['$addToSet' => ['current_offense_literal'=>'$current_offense_literal', 'current_offense_literal_value' =>'$current_offense_literal_value']]]
]
]);
});
非常感谢。就这样。