猫鼬:如何在架构上设置多个多键索引?

时间:2019-02-03 02:34:22

标签: mongodb mongoose mongoose-schema

我正在使用Mocha测试在Node中运行的Mongoose模型,在其中设置了2个多键索引,例如:

// eventsModel.js
// ...
// ...
const Indexes = require('./indexes')
EventSchema.index( Indexes.active(), {name: 'Active', background: false})
EventSchema.index( Indexes.inactive(), {name: 'Inactive', background: false})
module.exports = mongoose.model('Event', EventSchema)

但是当我像这样运行测试时,它不起作用。 在插入文档之前,索引似乎没有及时完成,然后我的测试失败,并出现planner returned error: bad hint错误。

我必须注释掉那些shema.indexes并将其设置为:

const Indexes = require('./indexes')
let Events = require('./eventsModel')
await mongoose.connection.db.collection('events').createIndex(Indexes.active())
await mongoose.connection.db.collection('events').createIndex(Indexes.inactive())
await Events.insertMany(events)

然后它起作用。 但是我宁愿只调用Events.insertMany(events),而不必担心创建索引,因为它们已经在架构中设置了...

为什么在模式索引时它不起作用?

0 个答案:

没有答案