NamespaceExists: Collection already exists MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

MongoServerError: Collection db.users already exists (NamespaceExists)

createCollection (or an index/rename) targeted a name that is already in use.

Exact message you may see NamespaceExists: Collection already existsMongoServerError: Collection db.users already exists. NS: db.usersMongoServerError: collection already exists code 48 NamespaceExistsNamespaceExists: Collection already exists. NS: shop.orders
Seen on: MongoDB

Meaning

Ignore the error when creating collections idempotently (try/catch on code 48), or check listCollections first. In migrations use createCollection only if missing; with Mongoose Model.createCollection() rejects if the collection exists with different options.

Common causes

  • Collection created twice
  • Migration scripts not idempotent
  • Different options (capped/validator) on the existing collection
  • Rename target exists
  • Parallel startup creating the same collection

⚡ Quick fix

  1. Check db.getCollectionNames() before creating
  2. Catch error code 48 (NamespaceExists) and continue
  3. Use collMod to change options on an existing collection

Detailed fix by platform

mongosh

  1. if (!db.getCollectionNames().includes("users")) db.createCollection("users")

How to diagnose

  1. Exists — Already created?
  2. Options — Different?
  3. Script — Idempotent?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…Document failed validation — MongoDB: MongoServerError: Document failed validation (code 121) (MongoDB)Document failed valid…E11000 duplicate key — MongoServerError: E11000 duplicate key error collection: db.users index: email_1 dup key (Node.js)E11000 duplicate keyMongoDB Atlas: connection timed out — IP not whitelisted — MongoServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted (MongoDB)MongoDB Atlas: connec…NamespaceNotFound: ns not found — MongoServerError: ns not found (NamespaceNotFound) — dropping/renaming/collStats on a missing collection (MongoDB)NamespaceNotFound: ns…RuntimeError dict — RuntimeError: dictionary changed size during iteration (Python)RuntimeError dictSort/Group exceeded memory limit of 100 MB (allowDiskUse) — MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in (MongoDB)Sort/Group exceeded m…Namespace…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.