mongodb - 获取子数组的不同值作为附加属性
问题描述
例如,我们有一个包含如下文档的集合:
{
"_id" : "1",
"Category" : "11",
"Type" : 1,
"Active" : true,
"IsValid" : null,
.
.
.
.
"AllTags": ["Product", "Product", "Product", "Response", "Comment", "Response"]
}
我们如何输出与这些完全相同的文档,但其中将包含具有不同标签的附加字段:["Product", "Response", "Comment"]
。因此生成的文档将如下所示:
{
"_id" : "1",
"Category" : "11",
"Type" : 1,
"Active" : true,
"IsValid" : null,
.
.
.
.
"AllTags": ["Product", "Product", "Product", "Response", "Comment", "Response"],
"Tags": ["Product", "Response", "Comment"] //property calculated from AllTags
}
我正在尝试添加阶段 $AddFields
{ "$addFields" : { "Tags" : {"$reduce": { "input": "$AllTags", initialValue: [], in: {
$cond: { if: { "$$this": {$nin: ["$$value"]} } , then: { $concatArrays : ["$$value", ["$$this"]] }, else: ? }
} } } } }
但它给出了这个错误:"Invalid $addFields :: caused by :: Unrecognized expression '$$this'"
,而且我不知道在else
op $cond
.
解决方案
推荐阅读
- json - 使用 jq 创建带有字符串变量作为参数的 json
- xml - 如何获得两个xpath节点集的交集
- c# - 将嵌套对象转换为数组
- mysql - MySQL 8.0.22 指定为定义者('root'@'%')的用户不存在
- javascript - 在 Javascript 中评估表达式树
- reactjs - 如何使材料 ui 自动完成组件可点击链接的选项?
- .net-core - “HubCallerContext”不包含“请求”singnalr .netcore 的定义
- elasticsearch - 来自多个索引的 Elasticsearch 查询,每个索引都有痛苦
- ios - XCode 11.3.1,测试运行器在 CI 上执行测试之前失败
- git - 如何在 Windows 上创建离线 SVN 存储库备份?