26
MongoDBERRORCommonQuery ErrorHIGH confidence

The specified collection or view does not exist

What this means

This error means that an operation was attempted on a collection, view, or index that does not exist. It is typically caused by a typo in the collection name or by trying to query a collection before it has been created.

Why it happens
  1. 1A typo in the collection name in a `find`, `insert`, or other command
  2. 2Attempting to query a collection that was dropped or has not been created yet
  3. 3Connecting to the wrong database where the collection does not exist
  4. 4Trying to drop an index on a non-existent collection
How to reproduce

A query targets a collection named `userss` when the correct name is `users`.

trigger — this will error
trigger — this will error
db.users.insertOne({ name: "Alice" });
// The following command fails because of a typo in the collection name.
db.userss.find({ name: "Alice" });

expected output

MongoServerError: NamespaceNotFound: <database>.<collection> not found

Fix 1

Correct the Collection Name

WHEN The error is due to a typo.

Correct the Collection Name
// Corrected command
db.users.find({ name: "Alice" });

Why this works

Carefully check the spelling of the collection name in your code against the actual names in the database (using `show collections`).

Fix 2

Ensure the Collection Exists

WHEN The operation might run before the collection is created.

Ensure the Collection Exists
// Many drivers and commands will create the collection implicitly on first insert.
// To be explicit, you can use createCollection.
db.createCollection("new_collection");
db.new_collection.find({});

Why this works

While many write operations create collections automatically, read operations or more complex commands might not. You can explicitly create the collection first, or ensure your application's logic creates it via an insert before attempting to read from it.

What not to do

Create empty collections for every possible typo to suppress the error

This clutters the database with unused collections and hides the underlying bug in the application code. The error is useful for identifying incorrect collection names.

Sources
Official documentation ↗

mongodb/mongo src/mongo/base/error_codes.yml

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MongoDB errors