Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document#populate behaves differently with query#populate in mongoose 4.x #7302

Closed
bbqaaq opened this issue Dec 6, 2018 · 4 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@bbqaaq
Copy link

bbqaaq commented Dec 6, 2018

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

@vkarpov15 vkarpov15 added this to the 5.3.17 milestone Dec 12, 2018
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Dec 12, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will investigate ASAP

@bbqaaq
Copy link
Author

bbqaaq commented Dec 14, 2018

Thanks @vkarpov15
You make it as "needs repro script"? Do I need to provide more information?
I thought my code from the issue is already enough

@vkarpov15 vkarpov15 modified the milestones: 5.3.17, 5.4.1, 5.4.3 Dec 14, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reaching out, the reason for "needs repro script" here is mostly pedantic. For us, a repro means we have either a standalone script or github repo that we can run and confirm that there is an issue. Generally we fill in the below template:

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `X`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);
  await mongoose.connection.dropDatabase();
}

If you're willing to take the time to your code and sample data to the above template, that'll help us move faster on this issue.

@vkarpov15 vkarpov15 modified the milestones: 5.4.3, 5.4.5 Jan 3, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.4.5, 5.4.6 Jan 14, 2019
vkarpov15 added a commit that referenced this issue Jan 20, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.4.6, 4.13.18 Jan 20, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jan 20, 2019
@vkarpov15
Copy link
Collaborator

Confirmed, will release 4.13.18 tomorrow with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants