Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

35
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?

4 months 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.

4 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.