MongoServerError: Collection db.users already exists (NamespaceExists)
createCollection (or an index/rename) targeted a name that is already in use.
NamespaceExists: Collection already existsMongoServerError: Collection db.users already exists. NS: db.usersMongoServerError: collection already exists code 48 NamespaceExistsNamespaceExists: Collection already exists. NS: shop.orders 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
- Check
db.getCollectionNames()before creating - Catch error code 48 (NamespaceExists) and continue
- Use
collModto change options on an existing collection
Detailed fix by platform
mongosh
if (!db.getCollectionNames().includes("users")) db.createCollection("users")
How to diagnose
- Exists — Already created?
- Options — Different?
- 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:
Similar errors
- Atlas: bad auth: Authentication failed MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password)
- BSONObj size is invalid / exceeds maximum document size (16MB) MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB)
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
Most viewed in MongoDB
- MongoParseError MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
- VersionError: No matching document found for id Mongoose: VersionError: No matching document found for id "5f..." version 3 modifiedPaths "items"
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026