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

105
Views
Mongoose Add Extra Field To Object

I want to add new field to object.

var _u = await User.findOne({_id},{_id:0, name:1, username:1, group:1}).populate({
  path: 'group',
  select: '-_id title'
})
_u.type = 'user'
console.log(_u)

But hasn't got type field. Why I can't add new field? What should I do?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's because Mongosee hydrate it's responses, so it's not pure JavaScript object.

If you want to return pure JavaScript object, you have to add lean() method to the query. Also, that will improve the performance.

await User.findOne({ _id }, { _id:0, name:1, username:1, group:1 })
  .populate({
    path: 'group',
    select: '-_id title'
  })
  .lean()

You can find more info in the official docs.

almost 4 years ago · Santiago Trujillo 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!