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

Empty documents were incorrectly added when lean populating a nested undefined array #14098

Closed
2 tasks done
csy1204 opened this issue Nov 19, 2023 · 0 comments
Closed
2 tasks done
Milestone

Comments

@csy1204
Copy link
Contributor

csy1204 commented Nov 19, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.0.1

Node.js version

20.9.0

MongoDB server version

5.1

Typescript version (if applicable)

No response

Description

Ref: #8432

If a field in a nested schema is of type array but is an empty field, {} is incorrectly added when doing a lean populate.
An empty array is added just fine in a normal populate, but in a lean populate an empty document is unexpectedly added, making the type inconsistent.
For consistent results, lean populate should be improved to not add it at all.
I want to fix it myself.

Steps to Reproduce

const userSchema = new mongoose.Schema({
  fullName: String,
  company: String
});
const User = db.model('User', userSchema);

const fileSchema = new mongoose.Schema({
  _id: String,
  uploaderId: {
    type: mongoose.ObjectId,
    ref: 'User'
  }
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
fileSchema.virtual('uploadedBy', {
  ref: 'User',
  localField: 'uploaderId',
  foreignField: '_id',
  justOne: true
});

const contentSchema = new mongoose.Schema({
  memo: String,
  files: { type: [fileSchema], default: [] }
}, { toObject: { virtuals: true }, toJSON: { virtuals: true }, _id: false });

const postSchema = new mongoose.Schema({
  title: String,
  content: { type: contentSchema }
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
const Post = db.model('Test1', postSchema);

const user = await User.create({ fullName: 'John Doe', company: 'GitHub' });
await Post.create([
  { title: 'London-Paris' },
  {
    title: 'Berlin-Moscow',
    content: {
      memo: 'Not Easy',
      files: [{ _id: '123', uploaderId: user._id }]
    }
  }
]);

await Post.updateMany({}, { $unset: { 'content.files': 1 } });

const populatedRides = await Post.find({}).populate({
  path: 'content.files.uploadedBy',
  justOne: true
}).lean();

console.log(populatedRides[0].content);
// { files: { uploadedBy: null } }
console.log(populatedRides[1].content);
// { memo: 'Not Easy', files: { uploadedBy: null } }

As you can see above, files is expected to be an array, but the {} is being added incorrectly, giving us the wrong result altogether.

Expected Behavior

console.log(populatedRides[0].content);
// { }
console.log(populatedRides[1].content);
// { memo: 'Not Easy' }
csy1204 added a commit to csy1204/mongoose that referenced this issue Nov 19, 2023
vkarpov15 added a commit that referenced this issue Nov 19, 2023
fix(populate): fix curPath to update appropriately
@vkarpov15 vkarpov15 added this to the 8.0.2 milestone Nov 19, 2023
vkarpov15 added a commit that referenced this issue Nov 22, 2023
fix(populate): fix curPath to update appropriately
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants