如何使用Mongoose删除数据库? [英] How to drop a database with Mongoose?
本文介绍了如何使用Mongoose删除数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Node.js和Mongoose准备一个数据库创建脚本. 如何检查数据库是否已存在,如果存在,请使用Mongoose删除(删除)该数据库?
I'm preparing a database creation script in Node.js and Mongoose. How can I check if the database already exists, and if so, drop (delete) it using Mongoose?
我找不到用猫鼬掉落它的方法.
I could not find a way to drop it with Mongoose.
推荐答案
没有从猫鼬中删除集合的方法,最好的办法是删除其中一个的内容:
There is no method for dropping a collection from mongoose, the best you can do is remove the content of one :
Model.remove({}, function(err) {
console.log('collection removed')
});
但是有一种方法可以访问mongodb本机javascript驱动程序,该驱动程序可以用于此
But there is a way to access the mongodb native javascript driver, which can be used for this
mongoose.connection.collections['collectionName'].drop( function(err) {
console.log('collection dropped');
});
警告
在尝试备份之前进行备份,以防万一出问题!
Warning
Make a backup before trying this in case anything goes wrong!
这篇关于如何使用Mongoose删除数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文