将对象数组中的数组字段连接到 mongodb 聚合中的一个字符串字段
Concat array field inside an array of object into one string field in mongodb aggregate
我想将对象数组中的数组字段值连接到一个字符串字段中。
这是现有的文档格式:
{
"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" ] }
}
}
}
}
}
但是这个returns这个:
{
"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)
}
您可以使用 $reduce 将字符串数组转换为单个字符串:
db.collection.aggregate([
{
$addFields: {
notes: {
$reduce: {
input: "$notes.term",
initialValue: "",
in: {
$cond: [ { "$eq": [ "$$value", "" ] }, "$$this", { $concat: [ "$$value", "\n", "$$this" ] } ]
}
}
}
}
}
])
我想将对象数组中的数组字段值连接到一个字符串字段中。 这是现有的文档格式:
{
"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" ] }
}
}
}
}
}
但是这个returns这个:
{
"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)
}
您可以使用 $reduce 将字符串数组转换为单个字符串:
db.collection.aggregate([
{
$addFields: {
notes: {
$reduce: {
input: "$notes.term",
initialValue: "",
in: {
$cond: [ { "$eq": [ "$$value", "" ] }, "$$this", { $concat: [ "$$value", "\n", "$$this" ] } ]
}
}
}
}
}
])