# MongoServerError: Collection db.users already exists (NamespaceExists)

**Code:** `NamespaceExists: Collection already exists` · **Category:** MongoDB · **Seen on:** MongoDB

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/mongodb/mongodb-namespace-exists-collection-already-exists)
