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

Depopulate not working for populated docs inside array #10592

Closed
LiaanM opened this issue Aug 23, 2021 · 1 comment
Closed

Depopulate not working for populated docs inside array #10592

LiaanM opened this issue Aug 23, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@LiaanM
Copy link
Contributor

LiaanM commented Aug 23, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
A schema containing an array of documents with a nested reference inside of the document in the array does not depopulate correctly.

If the current behavior is a bug, please provide the steps to reproduce.
The following is a test to reproduce the error. The band.members and band.lead is depopulated correctly, but the band.embeddedMembers are not.

it('depopulate all should depopulate nested array population', async function() {

     const Person = db.model('Person', {
          name: String
      });

      const Band = db.model('Band', {
          name: String,
          members: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
          lead: { type: Schema.Types.ObjectId, ref: 'Person' },
          embeddedMembers: [{
              active: Boolean,
              member :{
                  type: Schema.Types.ObjectId, ref: 'Person'
              }
          }]
      });

      const people = [{ name: 'Axl Rose' }, { name: 'Slash' }];
      try {
          let docs = await Person.create(people);
          let band = {
              name: 'Guns N\' Roses',
              members: [docs[0]._id, docs[1]],
              lead: docs[0]._id,
              embeddedMembers: [{active:true,member:docs[0]._id},{active:false, member:docs[1]._id}]
          };
          band = await Band.create(band);
          await band.populate('members lead').populate('embeddedMembers.member').execPopulate();
          assert.ok(band.populated('members'));
          assert.ok(band.populated('lead'));
          assert.ok(band.populated('embeddedMembers.member'));
          assert.equal(band.members[0].name, 'Axl Rose');
          assert.equal(band.embeddedMembers[0].member.name, 'Axl Rose');
          band.depopulate();
          assert.ok(!band.populated('members'));
          assert.ok(!band.populated('lead'));
          assert.ok(!band.populated('embeddedMembers.member'));
          assert.ok(!band.embeddedMembers[0].member.name);
          return null
      } catch (err) {
          /* handle error */
          assert.ifError(err);
          return err;
      }
  });

What is the expected behavior?
The array with nested population should depopulate.
I suspect the $set is not taking into account the array indexes here :

this.$set(key, populatedIds);

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js -> v14.17.5
mongoose -> 5.13.8
MongoDB -> 5.0.2

@vkarpov15 vkarpov15 added this to the 5.13.9 milestone Aug 24, 2021
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 24, 2021
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 24, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Aug 24, 2021

The last console.log() returns false when it should return true

const mongoose = require('mongoose');
const {Schema} = mongoose;

const Person = mongoose.model('Person', {
    name: String
});

const Band = mongoose.model('Band', {
    name: String,
    members: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
    lead: { type: Schema.Types.ObjectId, ref: 'Person' },
    embeddedMembers: [{
        active: Boolean,
        member :{
            type: Schema.Types.ObjectId, ref: 'Person'
        }
    }]
});

const people = [{ name: 'Axl Rose' }, { name: 'Slash' }];

async function test() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true
      });
      await mongoose.connection.dropDatabase();

      
try {
    let docs = await Person.create(people);
    let band = {
        name: 'Guns N\' Roses',
        members: [docs[0]._id, docs[1]],
        lead: docs[0]._id,
        embeddedMembers: [{active:true,member:docs[0]._id},{active:false, member:docs[1]._id}]
    };
    band = await Band.create(band);
    await band.populate('members lead').populate('embeddedMembers.member').execPopulate();
    console.log(band.populated('members')); //true
    console.log(band.populated('lead')); // true
    console.log(band.populated('embeddedMembers.member')); // true
    console.log(band.members[0].name, 'Axl Rose'); // true
    console.log(band.embeddedMembers[0].member.name, 'Axl Rose'); // true
    band.depopulate();
    console.log(!band.populated('members')); // true
    console.log(!band.populated('lead')); // true
    console.log(!band.populated('embeddedMembers.member')); // true
    console.log(!band.embeddedMembers[0].member.name); // false
    return null
} catch (err) {
    /* handle error */
    console.log(err);
    return err;
}
}

test();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Aug 24, 2021
vkarpov15 added a commit that referenced this issue Sep 2, 2021
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

3 participants