建议使用Mongoose删除索引的方法是什么? [英] What is the recommended way to drop indexes using 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,我尝试使用改编自
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?
推荐答案
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屋!