78

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

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' }

vineet
  • 12,735
  • 10
  • 52
  • 73
  • 1
    You're not connecting to the database (using `mongoose.connect()`). – robertklep May 10 '16 at 10:51
  • mongoose doesn't have drop collection method. chekout [this](http://stackoverflow.com/a/10088410/2165143) answer for more info. – Rajesh Dhiman May 10 '16 at 10:52
  • yes, i have used mongoose.connect(). @robertklep – vineet May 10 '16 at 11:29
  • @RajeshDhiman, mongoose.connection.db.dropCollection method has used, http://stackoverflow.com/questions/11453617/mongoose-js-remove-collection-or-db?answertab=active#tab-top – vineet May 10 '16 at 11:33

4 Answers4

113

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

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.

Andrew Homeyer
  • 7,577
  • 4
  • 31
  • 26
6

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

For example: _dropCollection

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

Adam Rosenthal
  • 366
  • 4
  • 8
3

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

'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]);
            }
        }
    }
};
Lito
  • 1,154
  • 2
  • 14
  • 21
0

This is how I check the collection exists, before attempting to drop.

if (db.listCollections().toArray().includes(collection)) {
   await db.collection(collection).drop();
}
Dan
  • 1
  • 1