vin*_*eet 44 mongoose mongodb node.js
当我尝试删除集合时,Mongoose会抛出一个错误,即" MongoError:ns not found ".
这是我的猫鼬代码:
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
......
......
......
mongoose.connection.db.dropCollection("myCollection",function(err,affect){
console.log('err',err);
})
Run Code Online (Sandbox Code Playgroud)
错误:
错误{[MongoError:ns not found]
name:'MongoError',
message:'ns not found',
ok:0,
errmsg:'ns not found'}
And*_*yer 61
MongoError: ns not found
在对不存在的集合执行操作时发生.
例如,尝试在创建显式集合之前或在将文档添加到隐式创建集合的集合之前删除索引.
Ada*_*hal 10
Status(ErrorCodes::NamespaceNotFound, "ns not found");
当您尝试删除不存在的集合、视图或索引时抛出。
除此之外,在执行任何 CRUD 操作之前无需显式检查集合是否已存在。
这是我的 mongodb 连接接口,以避免 drop 收集错误:
'use strict';
module.exports = class {
static async connect() {
this.mongoose = require('mongoose');
await this.mongoose.connect(process.env.MONGODB_DSN, {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 5000,
useFindAndModify: false
}).catch(err => {
console.error('Database connection error: ' + err.message);
});
this.db = this.mongoose.connection.db;
return this.db;
}
static async dropCollection(list) {
if (list.constructor.name !== 'Array') {
list = [list];
}
const collections = (await this.db.listCollections().toArray()).map(collection => collection.name);
for (let i = 0; i < list.length; i++) {
if (collections.indexOf(list[i]) !== -1) {
await this.db.dropCollection(list[i]);
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22260 次 |
最近记录: |