【问题标题】:How to drop index using Mongoose如何使用 Mongoose 删除索引
【发布时间】:2016-03-30 18:23:18
【问题描述】:

猫鼬

Imagdata.dropIndexes( { "image_id": 1 }, function(err){
         if(err){
             res.send("error");
        }
        else{
            res.send("success");
        }
});

这是我用于删除字段“image_id”的索引的代码。当我尝试执行此函数时,它显示“TypeError:Imagdata.dropIndexes 不是函数”。如何解决这个问题...

【问题讨论】:

    标签: mongodb mongoose mongoose-schema


    【解决方案1】:

    您需要同步索引。查看此来源:https://mongoosejs.com/docs/api.html#model_Model.syncIndexes

    const schema = new Schema({ name: { type: String, unique: true } });
    const Customer = mongoose.model('Customer', schema);
    await Customer.collection.createIndex({ age: 1 }); // Index is not in schema
    // Will drop the 'age' index and create an index on `name`
    await Customer.syncIndexes();
    

    【讨论】:

      【解决方案2】:

      您需要使用模型的原始 collection 属性来访问底层 MongoDB API:

      Imagdata.collection.dropIndex(...);
      

      【讨论】:

        猜你喜欢
        • 2019-09-30
        • 2018-12-30
        • 2016-11-19
        • 2022-01-06
        • 2020-01-31
        • 2012-10-12
        • 2020-03-26
        • 2016-05-10
        相关资源
        最近更新 更多