Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: ns not found when try to drop collection

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

like image 534
vineet Avatar asked May 10 '16 10:05

vineet


People also ask

How do I drop a collection in node JS?

Drop Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. The drop() method takes a callback function containing the error object and the result parameter which returns true if the collection was dropped successfully, otherwise it returns false.

Does MongoDB Create collection if not exists?

If a collection does not exist, MongoDB creates the collection when you first store data for that collection. You can also explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules.


2 Answers

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.

like image 109
Andrew Homeyer Avatar answered Oct 12 '22 00:10

Andrew Homeyer


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.

like image 8
Adam Rosenthal Avatar answered Oct 11 '22 23:10

Adam Rosenthal