How to check if a collection exists and delete it?

I want to check a collection exists or not. If it exists delete it or if not exist make it.
for example:

if (collectionName) {
  delete collectionName;
} else {
  make(collectionName);
}

how to do this in mongodb?

The exact details on how to do it depends on the driver your are using.

1 Like

I am using 3.5.5 version

A driver is language specific, not version specific. Which language, java, js, rust, go?

i am using node js .

Just drop the collection every time, additional access to the collection will create it.

drop(options, callback){Promise}

lib/collection.js, line 1140

Drop the collection from the database, removing it permanently. New accesses will create a new collection.

http://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#drop

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.