Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
mongodb query result to csv supporting fields with multiple lines

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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda