mongodb - 将对象数组中的数组字段连接到mongodb聚合中的一个字符串字段中
问题描述
我想将一组对象中的数组字段值连接到一个字符串字段中。继承人现有的文档格式:
{
"no" : "123456789",
"date" : ISODate("2020-04-01T05:19:02.263+0000"),
"notes" : [
{
"_id" : ObjectId("5b55aabe0550de0021097bf0"),
"term" : "BLA BLA"
},
{
"_id" : ObjectId("5b55aabe0550de0021097bf1"),
"term" : "BLA BLA BLA"
},
{
"_id" : ObjectId("5b55aabf0550de0021097ed2"),
"term" : "BLA"
}
],
"client" : "John Doe"
}
所需文件格式:
{
"no" : "123456789",
"date" : ISODate("2020-04-01T05:19:02.263+0000"),
"notes" : "BLA BLA \n BLA BLA BLA \n BLA",
"client" : "John Doe"
}
$project 的尝试:
{ "$project": {
"notes": {
"$map": {
"input": "$notes",
"as": "u",
"in": {
"name": { "$concat" : [ "$$u.term", "\\n" ] }
}
}
}
}
}
但这会返回:
{
"no" : "123456789",
"date" : ISODate("2020-04-01T05:19:02.263+0000"),
"client" : "John Doe"
"notes" : [
{
"name" : "BLA \n"
},
{
"name" : "BLA BLA \n"
},
{
"name" : "BLA BLA BLA \n"
}
]
}
如何将其转换为所需的格式?任何想法将不胜感激!
编辑 :
如果我们尝试将数组字段值添加在一起,我们如何在不分组的情况下这样做呢?
现有格式:
{
"sale" : {
"bills" : [
{
"billNo" : "1234567890",
"billAmt" : NumberInt(1070),
"tax" : NumberInt(70)
}
]
},
"no" : "123456789",
"date" : ISODate("2020-04-01T05:19:02.263+0000")
}
必需的 :
{
"no" : "123456789",
"date" : ISODate("2020-04-01T05:19:02.263+0000"),
"total" : NumberInt(1140)
}
解决方案
推荐阅读
- azure - Azure 成功发布/部署后未显示 Web 服务
- c++ - 如何在 cpp20 实验中导入 C 断言
- java - Python IO 性能调优
- c# - Share Point 客户端对象模型 - UndoCheckOut 不起作用
- java - 为什么 Amazon SWF 工作流在调用 TryCatch.cancel() 后没有立即取消?
- sharepoint-online - 我想将 Sharepoint 网站的颜色更改为另一种自定义颜色
- python - Calculate change from paid amount
- java - Spring Boot bean 未使用 Bazel 添加到上下文中
- networking - 如何将 Kubernetes 服务暴露给 VPC 中的所有区域,而不暴露给外界
- namespaces - phpunit 类未在测试中加载