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

92
Visualizações
How to display the amount of documents in a collection in ejs? With mongodb

I am trying to display the amount of available documents that are in a collection. The collection name is "merchant1".

This is the backend:

router.get('/merchanttrading', ensureAuthenticated, (req, res) => {
const user = req.user;
merchant1.find({},  function (error, data) { 
res.render("merchanttrading", {data, user: req.user});
})
})

And this is what I am trying in the front end:

<% for (var i = 0; i < data.length; i++) { %>
<h2 id="test"><%= data[i].length%></h2>
<% } %>




<script>

<% for (var i = 0; i < data.length; i++) { %>
var documentamount = <%= data[i]%>
<% } %>


</script>

But this will just up ending beign blank. Rendering the entire document just works fine but what I want is to show the amount of current documents in the collection as a number and not the document itself.

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have found a solution after more digging I came across this: https://www.mongodb.com/docs/manual/reference/method/db.collection.count/

Then I used that like this:

router.get('/merchanttrading', ensureAuthenticated, (req, res) => {
const user = req.user;
merchant1.count({},  function (error, data) { 
res.render("merchanttrading", {data, user: req.user});
})
})

The solution seemed to easier then I thought and I was just over thinking it.

about 4 years ago · Juan Pablo Isaza Relatório
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