本文介绍了Mongo $ addTo设置一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个有效的查询:
$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添加一个附加值。例如,每个current_offense_literal也具有current_offense_literal_value,我想将其与current_offense_literal一起添加。
I just want to add an additional value to the addToSet above. For example Each current_offense_literal also has a current_offense_literal_value and I would like to add that along with 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'=>['$push'=> '$current_offense_literal_value']]]]
]
]);
});
但是我得到:
无法识别的表达式'$ current_offense_literal'。
But I am getting:Unrecognized expression '$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'=>'$current_offense_literal', 'current_offense_literal_value' =>'$current_offense_literal_value']]]
]
]);
});
这篇关于Mongo $ addTo设置一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!