【问题标题】:How do I drop an index in mongoose [duplicate]如何在猫鼬中删除索引[重复]
【发布时间】:2019-02-22 13:38:54
【问题描述】:

我有以下用户

的模型
var MyModel =  new Schema({
  name: String,
  firstName: { type: String, default: '', trim: true }
}, { timestamps: true })

当我从createdAt 字段中删除索引时

MyModel.dropIndex({ createdAt: 1 })

会报错

 TypeError: MyModel.dropIndex is not a function

猫鼬版 --> 5.2.14

【问题讨论】:

    标签: mongodb indexing mongoose


    【解决方案1】:

    dropIndex 是模型的原生 collection 对象上的一个方法,因此您需要从那里访问它:

    var schema =  new Schema({
      name: String,
      firstName: { type: String, default: '', trim: true }
    }, { timestamps: true })
    
    const MyModel = mongoose.model('modelname',schema)
    

    现在创建索引

    MyModel.collection.dropIndex({ createdAt: 1 })
    

    【讨论】:

    • 感谢您的回答。但仍然得到相同的TypeError: Cannot read property 'dropIndex' of undefined
    • @DarkKnight 确保 MyModel 是您的模型,而不是您问题代码中显示的架构。
    • 知道了,谢谢 johnnyHk
    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 2021-07-08
    • 2017-03-26
    • 2018-11-19
    • 2021-11-21
    • 2018-03-17
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多