如何使用 Mongoose 删除索引 [英] How to drop index using Mongoose

查看:40
本文介绍了如何使用 Mongoose 删除索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

猫鼬

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

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

This is the code I used for remove the index of a field 'image_id'. When I trying to execute this function It shows "TypeError: Imagdata.dropIndexes is not a function". How to fix this issue...

推荐答案

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

You need to sync indexes. Check out this source : 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();

这篇关于如何使用 Mongoose 删除索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆