mongodb - Mongodb $mergeObjects 使用变量属性作为对象键
问题描述
我目前正在编写一个聚合并有一个名为tags
复杂对象的数组,例如:{name: 'tag1'}
。
我想确保数组只包含 unique tags
。
{
$addFields: {
uniqueTags: {
$reduce: {
input: "$tags",
initialValue: {},
in: {$mergeObjects: {"$$this.name": "$$this"}}
}}
}},
上面的代码失败,因为我使用$$this.name
对象键。例如,如果我将其更改为其他内容,5
则聚合工作将变得完美。
解决方案
$addToSet
works for nested objects.
推荐阅读
- arrays - 根据另一个数组元素的类对数组进行排序
- python - Keras BatchNormalization 层:InternalError:cuDNN 启动失败
- sql - 获取 7 天前的记录
- java - 使用 CustomAdapter 和 OnItenClickListener。侦听器没有响应
- sql - 如何从子选择中选择不存在的列?
- android-studio-3.0 - 安装 Android Studio 3.2 后出现“AndroidSdkData.getSdkData 不能为空”错误
- python - 如何向量化类实例化以允许 NumPy 数组作为输入?
- java - 使用 xml 布局在 Android 中自定义包装视图
- python - 从仅包含它的 Keras 张量中提取原始值
- php - Simple html dom parser get tr from table