database schema_error ai_generated true

pymongo.errors.OperationFailure: ns not found, full error: {'ok': 0, 'errmsg': 'ns not found', 'code': 26}

ID: database/mongodb-namespace-not-found

Also available as: JSON · Markdown
93%Fix Rate
94%Confidence
42Evidence
2023-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7 active

Root Cause

The referenced collection or database namespace does not exist. This error occurs when running operations that require an existing collection (drop, createIndex on non-existent collection in certain contexts, listIndexes). MongoDB normally auto-creates collections on first insert.

generic

Workarounds

  1. 93% success Verify the database and collection names in the connection string and code
    Connect with mongosh and run: show dbs; use mydb; show collections; Verify the exact collection name (case-sensitive). Check the application code and connection string for the correct database name. Common mistake: connecting to 'test' database instead of the application database.
  2. 90% success Create the collection explicitly if it should exist but was dropped
    Run: db.createCollection('mycollection'); or simply insert a document: db.mycollection.insertOne({ init: true }); then db.mycollection.deleteOne({ init: true }); For operations like drop, check if the collection exists first: if (db.getCollectionNames().includes('mycollection')) { db.mycollection.drop(); }

Dead Ends

Common approaches that don't work:

  1. Explicitly creating the collection before every operation as a defensive measure 70% fail

    MongoDB auto-creates collections on first write. Explicitly creating collections before every operation adds unnecessary overhead and complexity. The real issue is usually that you are running the operation against the wrong database or collection name.

  2. Ignoring the error and assuming the collection will appear eventually 80% fail

    If the namespace is wrong (typo, wrong database), the collection will never appear. Silently ignoring the error means operations against the intended collection are not executing.

Error Chain

Leads to:
Preceded by:
Frequently confused with: