如何使用 MongoDB 聚合将多个字符串字段连接成单个字段?
How to concatenate multiple string fields into single field using MongoDB Aggregation?
假设我有以下格式的传入数据:
{
"Name": "Test"
"Location": "Whatever",
"customerServices": [
{
"id": "test",
"cusId": "test",
"adr": "Adr 1",
"serviceCounty": "Center",
"area": "village"
},
{
"id": "test",
"cusId": "test",
"adr": "adr2",
"serviceCounty": "West",
"area": "city"
},
{
"id": "test",
"cusId": "test",
"adr": "test",
"serviceCounty": "West",
"area": "test"
}
]
}
任何想法,如何编写聚合查询将:
- 创建一个名为“serviceAreas”的新字段。类型:列表
- 对于 'customerServices' 中的每一项。它将 select:adr、serviceCounty 和 area 字段。
- 将它们附加到一个字符串中并添加到新创建的 serviceAreas 字段中。
- 它会 select 并且只对不同的 serviceCounty 项目进行操作
所以最后的结果是这样的:
{
"Name": "Test"
"Location": "Whatever",
"customerServices": [
{
"id": "test",
"cusId": "test",
"adr": "Adr 1",
"serviceCounty": "Center",
"area": "village"
},
{
"id": "test",
"cusId": "test",
"adr": "adr2",
"serviceCounty": "West",
"area": "city"
},
{
"id": "test",
"cusId": "test",
"adr": "test",
"serviceCounty": "West",
"area": "test"
}
],
"serviceAreas": [
"Adr 1, Center, village", "adr2, West, city"
]
}
感谢任何帮助!
这是我尝试过的方法,但没有成功:
'serviceAreas': {
'$reduce': {
'input': '$serviceImpactHistory.event.services',
'initialValue': [],
'in': {
'$setUnion': [
'$$value', {'$concat': ['$$this.serviceCounty', '$$this.adr', '$$this.area']}
]
}
}
},
您不需要使用 $setUnion,只需使用 $concat
创建您的字符串
db.collection.aggregate([
{
$addFields: {
serviceAreas: {
"$reduce": {
"input": "$customerServices",
"initialValue": "",
"in": {
"$concat": [
"$$value",
"$$this.serviceCounty",
", ",
"$$this.adr",
", ",
"$$this.area",
", "
]
}
}
}
}
}
])
假设我有以下格式的传入数据:
{
"Name": "Test"
"Location": "Whatever",
"customerServices": [
{
"id": "test",
"cusId": "test",
"adr": "Adr 1",
"serviceCounty": "Center",
"area": "village"
},
{
"id": "test",
"cusId": "test",
"adr": "adr2",
"serviceCounty": "West",
"area": "city"
},
{
"id": "test",
"cusId": "test",
"adr": "test",
"serviceCounty": "West",
"area": "test"
}
]
}
任何想法,如何编写聚合查询将:
- 创建一个名为“serviceAreas”的新字段。类型:列表
- 对于 'customerServices' 中的每一项。它将 select:adr、serviceCounty 和 area 字段。
- 将它们附加到一个字符串中并添加到新创建的 serviceAreas 字段中。
- 它会 select 并且只对不同的 serviceCounty 项目进行操作
所以最后的结果是这样的:
{
"Name": "Test"
"Location": "Whatever",
"customerServices": [
{
"id": "test",
"cusId": "test",
"adr": "Adr 1",
"serviceCounty": "Center",
"area": "village"
},
{
"id": "test",
"cusId": "test",
"adr": "adr2",
"serviceCounty": "West",
"area": "city"
},
{
"id": "test",
"cusId": "test",
"adr": "test",
"serviceCounty": "West",
"area": "test"
}
],
"serviceAreas": [
"Adr 1, Center, village", "adr2, West, city"
]
}
感谢任何帮助!
这是我尝试过的方法,但没有成功:
'serviceAreas': {
'$reduce': {
'input': '$serviceImpactHistory.event.services',
'initialValue': [],
'in': {
'$setUnion': [
'$$value', {'$concat': ['$$this.serviceCounty', '$$this.adr', '$$this.area']}
]
}
}
},
您不需要使用 $setUnion,只需使用 $concat
创建您的字符串db.collection.aggregate([
{
$addFields: {
serviceAreas: {
"$reduce": {
"input": "$customerServices",
"initialValue": "",
"in": {
"$concat": [
"$$value",
"$$this.serviceCounty",
", ",
"$$this.adr",
", ",
"$$this.area",
", "
]
}
}
}
}
}
])