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

Populate virtual with lean and transform cause null in array #12739

Closed
1 task done
conduongtong opened this issue Nov 30, 2022 · 0 comments · Fixed by #12804
Closed
1 task done

Populate virtual with lean and transform cause null in array #12739

conduongtong opened this issue Nov 30, 2022 · 0 comments · Fixed by #12804
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@conduongtong
Copy link

conduongtong commented Nov 30, 2022

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.7.4

Node.js version

18.12.1

MongoDB version

6.0.2

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I have an example like this:

const test = async () => {
  const parentSchema = new Schema({ title: String });
  const childSchema = new Schema({ title: String, parent: { type: mongoose.Schema.Types.ObjectId, ref: 'Parent' } });

  parentSchema.virtual('children', {
    ref: 'Child',
    localField: '_id',
    foreignField: 'parent',
  });
  const Parent = mongoose.model('Parent', parentSchema);

  const Child = mongoose.model('Child', childSchema);

  await Parent.create({ title: 'parent' });
  await Child.create({ title: 'child' });
  console.log(
    await Parent.find()
      .lean()
      .populate({
        path: 'children',
        match: { title: 'child' },
        select: '-__v',
        strictPopulate: false,
        transform: doc => {
          if (doc?._id) {
           // do something
            return doc;
          }
          return doc;
        },
      }),
  );
};

If I use .lean() with transform in populate, result will return [null] like this:

[
  {
    _id: new ObjectId("6386cc026dd86392014c9863"),
    title: 'parent',
    __v: 0,
    children: [null]
  }
]

Expect output:

children: []

Please help!

@conduongtong conduongtong added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Nov 30, 2022
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Dec 4, 2022
@vkarpov15 vkarpov15 added this to the 6.7.6 milestone Dec 4, 2022
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Dec 5, 2022
vkarpov15 added a commit that referenced this issue Dec 15, 2022
vkarpov15 added a commit that referenced this issue Dec 16, 2022
fix(populate): avoid calling `transform` if there's no populate results and using lean
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
2 participants