2

I'm trying to drop a non existant collection and I get the following error:

MongoError: ns not found.

In a similar question, there is a link to the mongo code which shows that this is the expected behaviour:

MongoError: ns not found when try to drop collection

However, according to the mongo documentation, this method should return false if the collection does not exists:

https://docs.mongodb.com/manual/reference/method/db.collection.drop/#db.collection.drop

What am I missing?

Server version - 3.6.5, mongodb client (javascript) - 3.0.21

The commands I used:

await mongodb.collection('colname').drop()

and

mongodb.collection('colname').drop((err, res) => {
    console.log('err: ' + err + ', res: ' + res) // doesn't get called
})
ayun12
  • 479
  • 1
  • 4
  • 9
  • You have linked to another answer. That answer includes everything you need. – mbuechmann Jun 21 '18 at 09:54
  • The official documentation says something else. Possible answers to this question may solve this contradiction (bug in the documentation, etc.) – ayun12 Jun 21 '18 at 09:59
  • Follow your first link and read the answer. It is all there. – mbuechmann Jun 21 '18 at 10:01
  • I read it carefully and the documentation was not mentioned – ayun12 Jun 21 '18 at 10:48
  • Ok. It seems I am myself a bit confused. For clarification: Which command did you execute exactly? – mbuechmann Jun 21 '18 at 11:20
  • 1
    I added the code to the question. Few minutes ago I paid attention that the documentation refers to the 'shell command'. It's in javascript but it's not the driver api maybe that's the cause for the different behaviour. confusing.. – ayun12 Jun 21 '18 at 11:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173555/discussion-between-mbuechmann-and-ayun12). – mbuechmann Jun 21 '18 at 11:59

1 Answers1

4

You link refers to the command interface of the mongo client. It uses javascript but is an application that has its own REPL. The documentaion is correct.

The command you are using is from the official mongodb node package. The behavior of these commands are different than those on the mongo client. The documentation concerning your usage is here: http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#drop

BTW, the first parameter is an option object, the second would be the callback.

The callback you provide is only called on a successful mongodb query. When the collection does not exist (like in this case), the callback is not executed. But this function returns a promise, which can be used to handle any error:

mongodb.collection('colname').drop().then(function () {
    // success
}).catch(function () {
    // error handling
})
mbuechmann
  • 5,413
  • 5
  • 27
  • 40