DBAtlas · Database operations atlas

48 NamespaceExists: Namespace (collection) already exists

MongoDB

What does it mean?

The collection (namespace) you are trying to create already exists. MongoDB reports that it is present, so there is no need to create a new collection with the same name and options.

Why does it happen?

Trying to re-create an existing collection with createCollection (possibly with different options); schema/setup scripts run more than once; or attempting to redefine an existing name as a capped collection or with special options.

How to fix it

Usually safe to ignore; it means the collection already exists. Make your setup scripts idempotent: check existence before creating (db.getCollectionNames().includes('name')). To change the options you must drop and re-create the collection (back up the data first): db.my_collection.drop() then createCollection.

Open in DBAtlas →

More from this engine