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

wrong partialFilterExpression index with discriminators #7634

Closed
egorovli opened this issue Mar 23, 2019 · 0 comments
Closed

wrong partialFilterExpression index with discriminators #7634

egorovli opened this issue Mar 23, 2019 · 0 comments
Milestone

Comments

@egorovli
Copy link
Contributor

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

A bug.

What is the current behavior?

The docs say it is possible to use three parameters to Model.discriminator(). If the third one is specified, it is used as a value for the discriminator. However the generated index in MongoDB uses the first parameter (the model name).

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
)

But if I do db.payments.getIndexes() I get:

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

So basically the model name (PaypalPayment) is used as a partialFilterExpression instead of the supplied discriminator value (paypal). This way the unique constraint doesn't work at all.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node 11.12.0
Mongoose 5.4.19
MongoDB 4.0.6

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