如何使用 Mongoose 删除数据库? [英] How to drop a database with Mongoose?

查看:37
本文介绍了如何使用 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.

推荐答案

没有办法从 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屋!

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