建议使用Mongoose删除索引的方法是什么? [英] What is the recommended way to drop indexes using Mongoose?

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

问题描述

我需要为MongoDB数据库创建几个部署脚本,例如数据迁移和固定装置,但是我找不到有关如何使用Mongoose API删除索引的足够信息.使用官方的MongoDB API时,这很简单:

I need to create several deployment scripts like data migration and fixtures for a MongoDB database and I couldn't find enough information about how to drop indexes using Mongoose API. This is pretty straight-forward when using the official MongoDB API:

要删除指定集合上的所有索引,请执行以下操作:

db.collection.dropIndexes();

To delete all indexes on the specified collection:

db.collection.dropIndexes();

但是,我想为此使用Mongoose,我尝试使用改编自executeDbCommand /bvdS3B_oNcM"rel =" noreferrer>这篇文章,但没有成功:

However, I would like to use Mongoose for this and I tried to use executeDbCommand adapted from this post, but with no success:

mongoose.connection.db.executeDbCommand({ dropIndexes: collectionName, index: '*' },
  function(err, result) { /* ... */ });

我应该将官方的MongoDB API用于Node.js,还是只是错过了这种方法?

Should I use the official MongoDB API for Node.js or I just missed something in this approach?

推荐答案

要通过Mongoose模型对该集合进行此操作,可以调用

To do this via the Mongoose model for the collection, you can call dropAllIndexes of the native collection:

MyModel.collection.dropAllIndexes(function (err, results) {
    // Handle errors
});

更新

dropAllIndexes在本机驱动程序的2.x版本中已弃用,因此

dropAllIndexes is deprecated in the 2.x version of the native driver, so dropIndexes should be used instead:

MyModel.collection.dropIndexes(function (err, results) {
    // Handle errors
});

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

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