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' }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With