Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!