php - Mongo $addToSet 一个数组
问题描述
我有一个工作查询:
$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 一起添加。
我尝试了什么:
$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'。
解决方案
尝试这样做,
$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']]]
]
]);
});
推荐阅读
- azure-functions - 如何在 Azure 函数中使用多个可选参数
- ios - UISegmented Control 内部视图应在视图显示后调用 API
- html - 如何将内容与 css 对齐
- c# - 从 ASP.NET 迁移到 .NET Core,我的 API 现在非常慢。我可以做些什么来诊断?
- generics - 用于 Rust 中多个泛型转换的 GADT
- c# - WPF 数据网格 MinWidth=auto MaxWidth=*
- c# - .NET 和 SOAP 标记问题(将对象序列化为 XML)
- reactjs - 如何使用动态路由在页面中触发相同的 .js?
- objective-c-runtime - userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: 未触发
- python - 如何在python中使用列表中的两个条件进行for循环