Mongoose: Indexing in embedded discriminators array is not work

Created on 20 May 2018  路  2Comments  路  Source: Automattic/mongoose

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

What is the current behavior?
Indexing in embedded discriminators array is not work

If the current behavior is a bug, please provide the steps to reproduce.

const eventSchema = new mongoose.Schema(
  {
    message: {
      type: String,
      index: true,
    },
  },
  { discriminatorKey: 'kind', _id: false },
);

const batchSchema = new mongoose.Schema(
  {
    events: [eventSchema],
  },
);

const docArray = batchSchema.path('events');

const Clicked = docArray.discriminator('Clicked', new mongoose.Schema(
  {
    element: {
      type: String,
      required: true,
      index: true,
    },
  },
  { _id: false },
));

const Purchased = docArray.discriminator('Purchased', new mongoose.Schema(
  {
    product: {
      type: String,
      required: true,
      index: true,
    },
  },
  { _id: false },
));

const Batch = mongoose.model('EventBatch', batchSchema);

What is the expected behavior?
Mongoose should trigger ensureIndex on 3 field

  • events.message
  • events.element
  • events.product

Only one field which Mongoose trying to index is events.message
Mongoose: eventbatches.ensureIndex({ 'events.message': 1 }, { background: true })

Please mention your node.js, mongoose and MongoDB version.
Node.js v8.9.3
Mongoose v5.1.1
MongoDB v3.6

confirmed-bug

All 2 comments

Thanks for reporting, will investigate ASAP :+1:

Thank you for your help :)

Was this page helpful?
0 / 5 - 0 ratings