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

124
Views
How to retrieve data from MongoDb Atlas and display in an ejs file using mongoose and Nodejs

Thanks for the help in advance.

I am trying to retrieve data from my database- myFirstDatabase and collection named as 'shipment' in mongondb. This is a nested schema but I am only interested in the parent data for now. I have this code which retrieves data to the console log. But how can I display or access the data in my orders.ejs file?

Shipment.find({}, function (err, data) {
  if (err) throw err

  console.log(data)
})


MongoDB connected...
[
  {
    _id: new ObjectId("61353311261da54811ee0ca5"),
    name: 'Micky Mouse',
    phone: '5557770000',
    email: 'g@gmail.com',
    address: {
      address: '10 Merrybrook Drive',
      city: 'Herndon',
      state: 'Virginia',
      zip: '21171',
      country: 'United States',
      _id: new ObjectId("61353311261da54811ee0ca6")
    },
    items: {
      car: 'Honda Pilot 2018',
      boxItem: '3 uHaul boxes',
      furniture: 'None',
      electronics: '1 50" Samsung TV',
      bags: '2 black suites cases',
      _id: new ObjectId("61353311261da54811ee0ca7")
    },
    date: 2021-09-05T21:13:53.484Z,
    __v: 0
  }
]

This is the ejs file, a table I am trying to populate the data i get from my mongodb

<div class="mt-5 m-auto>
  <h3 class="mt-5">This is the order table</h3>
  <%- include ("./partials/messages"); %>
  <div class="col-sm">
    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th>#</th>
          <th>Customer</th>
          <th>Address</th>
          <th>City</th>
          <th>State</th>
          <th>Zip</th>
          <th>Phone</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <tr class="success">
          <td>1</td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

server.js

const express = require('express')
const mongoose = require('mongoose')
const Shipment= require('./models/shipment')
const app = express()

mongoose.connect('mongodb://localhost/myFirstDatabase ', {
    useNewUrlParser: true, useUnifiedTopology: true
})

app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended:false }))

app.get('/', async (req,res) => {
    const data= await Shipment.find()
    res.render('index', { data: data})
})

app.listen(process.env.PORT || 5000);

index.ejs

below is part of your ejs file

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>#</th>
            <th>Customer</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
            <th>Zip</th>
            <th>Phone</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <% data.forEach((e, index)=> { %> 
            <tr>
                <td><%= index %></td>
                <td><%= e.Customer %></td>
                <td><%= e.Address %></td>
                <td><%= e.City %></td>
                <td><%= e.State %></td>
                <td><%= e.Zip %></td>
                <td><%= e.Phone %></td>
                <td><%= e.Status %></td>
            </tr>
        <% }) %>  
    </tbody>
</table>
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!