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

"strictPopulate" not working after upgrading from v5 to v6 #13863

Closed
2 tasks done
sambhav2612 opened this issue Sep 15, 2023 · 3 comments · Fixed by #13996
Closed
2 tasks done

"strictPopulate" not working after upgrading from v5 to v6 #13863

sambhav2612 opened this issue Sep 15, 2023 · 3 comments · Fixed by #13996
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@sambhav2612
Copy link

sambhav2612 commented Sep 15, 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

6.12.0

Node.js version

16.20.0

MongoDB server version

6.0.10

Typescript version (if applicable)

4.7.4

Description

Hi,

We are trying to upgrade our atlas cluster from v5 to v6, for which, we tried upgrading mongoose to prevent any server-level compatibility issues however we keep getting errors around strictPopulate.

Seemingly the type-declaration for global override has been already added (ref: #11006) which did not work.

This is inside NestJS v7 (@nestjs/mongoose: ^7.2.4)

Steps to Reproduce

Example Code Throwing Error:

async migrateSomething(): Promise<any> {
    let response = await this.modelX
      .find(filter)
      .populate({
        path: 'a b c', // direct keys available in schema for X
        populate: {
          path: 'd c', // direct keys available in schema for A (not X)
          populate: 'e', // indirect keys outside of schema for X
          strictPopulate: false, // <== not working
        },
      });
    
...
...
...
...

  }

Expected Behavior

Should not throw any error or allow server to run while gracefully putting a warning

@IslandRhythms
Copy link
Collaborator

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Sep 18, 2023
@vkarpov15 vkarpov15 added this to the 7.5.5 milestone Sep 26, 2023
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 26, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.2, 7.6.3, 7.6.4 Oct 13, 2023
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 18, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Oct 18, 2023

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  location: String,
  occupation: String,
});

const ASchema = new mongoose.Schema({
  name: String,
  age: Number,
  weight: Number,
  dModel: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'DModel'
  },
  fModel: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'FModel'
  }
});

const XSchema = new mongoose.Schema({
  name: String,
  car: String,
  make: String,
  model: String,
  aModel: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'AModel'
  },
  testModel: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Test'
  },
  cModel: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'CModel'
  }
});

const CSchema = new mongoose.Schema({
  name: String,
  hobbies: String
})

const DSchema = new mongoose.Schema({
  name: String,
});

const FSchema = new mongoose.Schema({
  name: String,
});

const GSchema = new mongoose.Schema({
  name: String
});

const Test = mongoose.model('Test', testSchema);
const AModel = mongoose.model('AModel', ASchema);
const XModel = mongoose.model('XModel', XSchema);
const CModel = mongoose.model('CModel', CSchema);
const DModel = mongoose.model('DModel', DSchema);
const FModel = mongoose.model('FModel', FSchema);
const GModel = mongoose.model('GModel', GSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const gDoc = await GModel.create({
    name: 'G-Man'
  });
  const dDoc = await DModel.create({
    name: 'Dirty Dan',
  });

  const fDoc = await FModel.create({
    name: 'Filthy Frank',
  });

  const testDoc = await Test.create({
    name: 'Test Testserson',
    location: 'Florida',
    occupation: 'Tester'
  });

  const aDoc = await AModel.create({
    name: 'A-men',
    age: 4,
    weight: 404,
    dModel: dDoc._id,
    fModel: fDoc._id
  });

  const cDoc = await CModel.create({
    name: 'C-ya',
    hobbies: 'Leaving'
  });

  await XModel.create({
    name: 'XCOM',
    aModel: aDoc._id,
    cModel: cDoc._id,
    testModel: testDoc._id
  });

  const res = await XModel.find().populate({
    path: 'aModel testModel cModel',
    populate: {
      path: 'dModel fModel',
      populate: 'gModel',
      strictPopulate: false
    }
  });

  console.log('what is res', res[0]);
  console.log('==============================');
  console.log('what is amodel in the doc', res[0].aModel)

  console.log('done');

}

run();

@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 Oct 18, 2023
@IslandRhythms IslandRhythms linked a pull request Oct 20, 2023 that will close this issue
@vkarpov15
Copy link
Collaborator

I did some debugging and it looks like this is expected behavior. You need to apply strictPopulate on the e populate, as written you've set strictPopulate on the d c populate. Do the following instead:

      .populate({
        path: 'a b c', // direct keys available in schema for X
        populate: {
          path: 'd c', // direct keys available in schema for A (not X)
          populate: { path: 'e', strictPopulate: false } // indirect keys outside of schema for X
        },
      });

vkarpov15 added a commit that referenced this issue Oct 20, 2023
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

Successfully merging a pull request may close this issue.

3 participants