2

猫鼬

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

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

4

2 回答 2

8

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

Imagdata.collection.dropIndex(...);
于 2015-12-24T09:51:32.263 回答
3

您需要同步索引。查看此来源: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();
于 2020-08-28T13:02:05.137 回答