Following are two documents in my collection:
{
"_id": {
"$oid": "5f48e358d43721376c397f54"
},
"heading": "this is heading",
"tags": ["tag1","tag2","tag3"],
"categories": ["projA", "projectA2"],
"content": ["This", "is", "the", "content", "of", "the", "document"],
"timestamp": 1598612312.506219,
"lang": "en"
}
and
{
"_id": {
"$oid": "5f48e358d43721376c397f53"
},
"heading": "this is heading",
"tags": "tag1,tag2,tag3",
"categories": "projA, projectA2",
"content": "This is the content of the document",
"timestamp": 1598612312.506219,
"lang": "en"
}
I want to write a query that concatenates the content
element for first document and prints the second as is(as it is not an array).
I am using the following script to concatenate but it is giving error on the second document saying that it needs to be an array and it is not. I cannot figure out how to use switch statement or nested if conditions in this scenario:
"$addFields": {
content: {
"$reduce": {
"input": "$content",
"initialValue": "",
"in": {
"$cond": {
"if": { "$eq": [ { "$indexOfArray": [ "$content", "$$this" ] }, 0 ] },
"then": { "$concat": [ "$$value", "$$this" ] },
"else": { "$concat": [ "$$value", "\n", "$$this" ] }
}
}
}
}
}
I am very new to mongo DB so any help will be appreciated.