I have a query in mongodb that formats the results into CSV. There are lots of lookup to other collections also.
My problem is, there are fields that contains multiple lines. I tried enclosing them with single/double quotes, but it still breaks the CSV (not formatted properly).
I can only use javascript coding that is supported by mongodb (version 4.2.3).
Example data:
"Grade 1", "Section Apple", 22, "N/A", "Mrs. Horrible"
"Grade 1", "Section Banana", 25, "N/A", "Mr. Perversion"
"Grade 1", "Section Cherry", 23, "
Noisy/Standing
--------------
1. Nilo Butay
2. Rowena D. Mahuwasan
3. Pedro Mahilig
4. Teresa Hubagan
", "Mrs. Unknown"
"Grade 1", "Section Dalandan", 20, "N/A", "Engr. Basic"
"Grade 1", "Section Elderberry", 25, "N/A", "Fr. Holiness"
Please note that section cherry contains data with multiple lines. This is breaking the CSV file/format.
If I remove this column, query result is rendered correctly in the CSV file.
Is there anyway via javascript, supported by mongodb, that this data can be rendered correctly?
This data is just an example, but there are hundreds or thousands records in the actual result. It is also possible that there will be another conversion into CSV from another collection or query.
Temporarily, I replaced the line endings to spaces so that the CSV file will not break.
Below is the sample code I used for removing the line endings:
var description = pr.description;
if (description) {
description = description.replace(/(\r\n|\n|\r)/gm, " ");
} else {
description = "";
}
The above code is just a work-around. What I want is to completely display the field value with multiple lines like in a normal spreadsheet. But I do not know how to format this data so that it will not break in the CSV file.