node.js MongoError:尝试删除集合时未找到 ns

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37136204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:11:30  来源:igfitidea点击:

MongoError: ns not found when try to drop collection

node.jsmongodbmongoose

提问by vineet

Mongoose throw an error i.e, "MongoError: ns not found" when i try to drop collection.

当我尝试删除集合时,猫鼬抛出一个错误,即“ MongoError: ns not found”。

Here is my mongoose code:

这是我的猫鼬代码:

var mongoose = require('bluebird').promisifyAll(require('mongoose'));
......
......
......   
mongoose.connection.db.dropCollection("myCollection",function(err,affect){
   console.log('err',err);

})

Error:

错误:

err { [MongoError: ns not found]
name: 'MongoError',
message: 'ns not found',
ok: 0,
errmsg: 'ns not found' }

err { [MongoError: ns not found]
name: 'MongoError',
message: 'ns not found',
ok: 0,
errmsg: 'ns not found' }

回答by Andrew Homeyer

MongoError: ns not foundoccurs when performing actions on collections that don't exist.

MongoError: ns not found在对不存在的集合执行操作时发生。

For example, attempting to drop indexes before an explicit collection creation has occurred or before adding a document to the collection which implicitly creates the collection.

例如,在显式集合创建发生之前或在将文档添加到隐式创建集合的集合之前尝试删除索引。

回答by Lito

This is my mongodb connection interface to avoid drop collection error:

这是我的mongodb连接接口,以避免丢弃收集错误:

'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]);
            }
        }
    }
};

回答by Adam Rosenthal

Status(ErrorCodes::NamespaceNotFound, "ns not found");is thrown when you try to drop a collection, a view or an index that doesn't exist.

Status(ErrorCodes::NamespaceNotFound, "ns not found");当您尝试删除不存在的集合、视图或索引时抛出。

For example: _dropCollection

例如: _dropCollection

Other than that, there is no need to explicitly check if a collection already exists before doing any CRUD operations.

除此之外,在执行任何 CRUD 操作之前不需要显式检查集合是否已经存在。