concatenate string values in array to a single field in DocumentDB

0

Suppose that I have a series of documents with the following format:

{
"_id": "3_0",
"values": \["Test1", "Test2"]
}

and I would like to obtain a projection of the array's values concatenated in a single field:

{
"_id": "3_0",
"values": "Test1_Test2"
}

I could use following when i use mongodb, however, reduce is not supported in documentDB and I could not get this to work.
db.collection.aggregate([
{ "$addFields": {
"values": {
"$reduce": {
"input": "$values",
"initialValue": "",
"in": {
"$cond": {
"if": { "$eq": [ { "$indexOfArray": [ "$values", "$$this" ] }, 0 ] },
"then": { "$concat": [ "$$value", "$$this" ] },
"else": { "$concat": [ "$$value", "_", "$$this" ] }
}
}
}
}
}}
])

Could you please tell me if there's a workout. Thank you

Edited by: heshananupama on Feb 1, 2021 11:41 AM

Edited by: heshananupama on Feb 1, 2021 11:41 AM

Edited by: heshananupama on Feb 1, 2021 11:41 AM

Edited by: heshananupama on Feb 1, 2021 11:42 AM

질문됨 3년 전209회 조회
1개 답변
0

DocumentDB does support $reduce. for

rs0:PRIMARY> db.testc.find()
{ "_id" : "3_0", "values" : [ "Test1", "Test2" ] }
rs0:PRIMARY> db.testc.aggregate([ { "$addFields": { "values": { "$reduce": { "input": "$values", "initialValue": "", "in": { "$cond": { "if": { "$eq": [ { "$indexOfArray": [ "$values", "$$this" ] }, 0 ] }, "then": { "$concat": [ "$$value", "$$this" ] }, "else": { "$concat": [ "$$value", "_", "$$this" ] } } } } } }} ])
{ "_id" : "3_0", "values" : "Test1_Test2" }
rs0:PRIMARY> db.version()
4.0.0

AWS
Anva
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠