document#populate
behaves differently with query#populate
in mongoose 4.x #7302
Description
Do you want to request a feature or report a bug?
Probably a bug.
What is the current behavior?
In the latest version of mongoose 4.x,document#populate
behaves differently with query#populate
If the current behavior is a bug, please provide the steps to reproduce.
When using the query#populate
, the document's populated field can be modified normally:
let potter = await Book.findOne({ name: 'Potter' }).populate('owner').exec()
console.log('before potter = ', potter)
potter.owner = {}
console.log('after potter = ', potter)
and the result is this
before potter = { _id: 5c08b95a0590e665915a46d1,
name: 'Potter',
owner:
{ _id: 5c08b95a0590e665915a46cf,
name: 'Peter',
age: '12',
__v: 0 },
__v: 0 }
after potter = { _id: 5c08b95a0590e665915a46d1,
name: 'Potter',
owner: { _id: 5c08cd6227b7516865831774 },
__v: 0 }
However, when using document#populate
, the owner field will become unchangeable unless the new value is a mongoose document or null
let potter = await Book.findOne({ name: 'Potter' }).exec()
await potter.populate('owner').execPopulate()
console.log('before potter = ', potter)
potter.owner = {}
console.log('after potter = ', potter)
and the result is this
before potter = { _id: 5c08b95a0590e665915a46d1,
name: 'Potter',
owner:
{ _id: 5c08b95a0590e665915a46cf,
name: 'Peter',
age: '12',
__v: 0 },
__v: 0 }
after potter = { _id: 5c08b95a0590e665915a46d1,
name: 'Potter',
owner:
{ _id: 5c08b95a0590e665915a46cf,
name: 'Peter',
age: '12',
__v: 0 },
__v: 0 }
This problem isn't appear when I was still using mongoose 4.7.8, it just appear when I try to push the mongoose version to latest 4.x. For mongoose 5.x, this problem don't appear too.
Please mention your node.js, mongoose and MongoDB version.
Node 8.12.0
Mongoose 4.13.17
MongoDB 3.2.21
Activity