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

fix modelName being used as value to partialFilterExpression index #7635

Merged
merged 1 commit into from
Mar 25, 2019
Merged

fix modelName being used as value to partialFilterExpression index #7635

merged 1 commit into from
Mar 25, 2019

Conversation

egorovli
Copy link
Contributor

Summary

The issue is described here: #7634. If the third parameter is used in a Model.discriminator() method it should be used as a value to partialFilterExpression index instead of the model.modelName.

Examples

Consider the following example:

let BasePaymentSchema = new mongoose.Schema({
  amount: mongoose.Types.Decimal128
}, {
  discriminatorKey: 'provider'
})

let BasePayment = mongoose.model('Payment', BasePaymentSchema)

let PaypalPaymentSchema = new mongoose.Schema({
  transactionId: {
    type: String,
    unique: true
  }
})

let PaypalPayment = BasePayment.discriminator(
  'PaypalPayment',
  PaypalPaymentSchema,
  'paypal' // This value should be in discriminator field
)

Before the fix db.payments.getIndexes() returns:

{
  "v": 2,
  "unique": true,
  "key": {
    "transactionId": 1
  },
  "name": "transactionId_1",
  "ns": "my-database.payments",
  "background": true,
  "partialFilterExpression": {
    "provider": "PaypalPayment"
  }
}

After the fix:

{
  "v": 2,
  "unique": true,
  "key": {
    "transactionId": 1
  },
  "name": "transactionId_1",
  "ns": "my-database.payments",
  "background": true,
  "partialFilterExpression": {
    "provider": "paypal"
  }
}

@@ -1494,8 +1494,14 @@ function _decorateDiscriminatorIndexOptions(model, indexOptions) {
if (model.baseModelName != null && indexOptions.unique &&
!('partialFilterExpression' in indexOptions) &&
!('sparse' in indexOptions)) {

const value = (
model.schema.discriminatorMapping &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really familiar with mongoose internals, maybe this && check isn't necessary.

@vkarpov15 vkarpov15 added this to the 5.4.20 milestone Mar 25, 2019
@vkarpov15 vkarpov15 merged commit 60169b6 into Automattic:master Mar 25, 2019
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

Successfully merging this pull request may close these issues.

None yet

2 participants