Joh*_*yHK 68
是的,虽然你是通过本机MongoDB驱动程序而不是Mongoose本身完成的.假设一个必需的,连接的mongoose变量,本机Db对象可以通过mongoose.connection.db,该对象提供dropCollection和dropDatabase方法.
// Drop the 'foo' collection from the current database
mongoose.connection.db.dropCollection('foo', function(err, result) {...});
// Drop the current database
mongoose.connection.db.dropDatabase(function(err, result) {...});
Run Code Online (Sandbox Code Playgroud)
Dav*_*lsh 51
现在可以在Mongoose中完成.
MyModel.collection.drop();
Run Code Online (Sandbox Code Playgroud)
帽子提示:https://github.com/Automattic/mongoose/issues/4511
对于那些使用mochajs测试框架并希望在每次测试后清理所有数据库集合的人,可以使用以下使用async/await:
afterEach(async function () {
const collections = await mongoose.connection.db.collections()
for (let collection of collections) {
await collection.remove()
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51452 次 |
| 最近记录: |