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

307
Views
Mongodb not returning embedded document as an object when using find() in node.js with express but works fine with findById()

schema:

const mongoose = require ('mongoose');

const workSchema = new mongoose.Schema ({
    role: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Staff_roles'
    },
    movie: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Movie'
    }
})

const staffSchema = new mongoose.Schema ({
    name: {
        type: String,
        required: true,
    },
    works : [workSchema],
    summary: {
        type: String
    }
});

module.exports = mongoose.model ('Staff', staffSchema)

I need to implement this in a different page for movies where I need to show the properties of the fields inside "works".

Works is shown as a normal object with the embedded document displaying correctly when using findById() but it only shows [object] for works when using find().

I need to run the find query as Staff.find({ "works.movies._id" :movie.id }) later on so I need to use find later on and it keeps returning it as [object] which makes it impossible to access the contents of work later on the .ejs file for it.

I already tried populating it with 'works' but that doesn't do anything.

staff router code and result using console logs when using findById():

const staff = await Staff.findById(req.params.id).populate('works.movie works.role').exec()
console.log (staff)
{
  _id: new ObjectId("61850be2d9272be145267f4d"),
  name: 'hideo',
  works: [
    {
      role: [Object],
      movie: [Object],
      _id: new ObjectId("61850be9d9272be145267f55")
    }
  ],
  __v: 0
}

staff router code and result using console logs when using find():

const staff = await Staff.find({id:req.params.id}).populate('works.movie works.role').exec()
console.log (staff)
[
  {
    _id: new ObjectId("61850be2d9272be145267f4d"),
    name: 'hideo',
    works: [ [Object] ],
    __v: 0
  }
]
about 4 years ago · Juan Pablo Isaza
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!