- Error code
- 26
- Code name
NamespaceNotFound- Product
- MongoDB
- Versions
- MongoDB 7.0 and 8.0 (wordings checked against the 8.0 source)
- Category
- Indexes & Collections
- Last reviewed
- 24 October 2026
On this page
What it means
Error 26 (NamespaceNotFound) means a command that needs an existing collection, such as listing or dropping its indexes, collMod or renameCollection, was given a database.collection name (a namespace) that does not exist.
A namespace is database.collection, shown in the message as shop.order. MongoDB creates collections implicitly on the first insert or index creation, and a query on a collection that does not exist is not an error: the server answers it with an empty plan and you get no documents. Commands that act on an existing collection's metadata cannot do that, so they return 26: listIndexes (getIndexes()), dropIndexes, collMod, renameCollection and $geoNear, among others.
The name is almost always the problem rather than the data: a typo or a singular/plural mix-up (order and orders), different letter case (collection names are compared exactly, so Orders is another collection), or the right collection in the wrong database because the connection string or use pointed elsewhere. Note that a find() with a misspelt name silently returns nothing, so the first visible symptom is often this error from an index or admin command.
Some operations deliberately succeed on a missing collection: the documentation says db.collection.drop() still returns true when the collection does not exist. If you need a collection to be present, check for it explicitly rather than relying on errors.
Common causes
- A misspelt or wrongly cased collection name in code, a script or a migration.
- The connection uses a different database than expected, for example a database in the connection string that differs from the one the script assumes.
- The collection was dropped or renamed by another process, or has not been created yet in a new environment.
- A migration runs collMod or renameCollection before the step that creates the collection.
- A view name or time series name used with a command that needs the underlying collection.
Diagnose it
Read-only These mongosh commands only read data, indexes, statistics and server state (find, aggregate without $out or $merge, getIndexes, stats, currentOp, rs.status). They do not change data, users or settings, so they are safe to run first.
Which database is this session using?
db.getName() The current database name. It must match the part of the namespace before the dot in the message.
List the collections that do exist
db.getCollectionNames()
db.getCollectionInfos({ name: /order/i }) The real collection names. A near match such as orders or Order shows a typo or a case mismatch.
Find the collection in another database
db.adminCommand({ listDatabases: 1, nameOnly: true })
db.getSiblingDB("shop_prod").getCollectionNames() The databases on the server, then the collections in a candidate database. If the collection is there, fix the database name in the connection string or script.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Use the correct database and collection name
Client or network sideCorrect the name in code, and address the database explicitly in scripts instead of relying on the current one.
const shop = db.getSiblingDB("shop")
shop.orders.getIndexes()
Create the collection before commands that need it
Changes schemaIn migrations and set-up scripts, create the collection (with its options, such as a validator) before collMod or index changes run against it.
db.createCollection("orders")
db.runCommand({ collMod: "orders", validationLevel: "moderate" })
Make scripts check for the collection first
Client or network sideFor steps that should do nothing when the collection is absent, test for it, or treat code 26 as "nothing to do" only in that step. Check the code, not the message text.
if (db.getCollectionNames().includes("order_archive")) {
db.order_archive.getIndexes()
}
Examples
Listing indexes on a misspelt collection
Not reproduced here: no MongoDB server is installed for this site. The text is the one built by the listIndexes command in MongoDB 8.0 (list_indexes.cpp), shown as mongosh prints a server error; the names are this example's.
use shop
db.order.getIndexes() // the collection is called orders
MongoServerError: ns does not exist: shop.order
Renaming a collection that is not there
Not reproduced here: no MongoDB server is installed for this site. The text is built in MongoDB 8.0's rename_collection.cpp when the source namespace has no collection; on a sharded cluster the rename runs through a different path and the wording may differ.
use shop
db.order.renameCollection("orders_2025")
MongoServerError: Source collection shop.order does not exist
How to prevent it
Keep collection names in one constant or model definition rather than as strings scattered through code, and put the database name in configuration that is checked at start-up.
Order migrations so that collections are created before they are altered, and make each step safe to run twice.
Common questions
What does "ns does not exist" mean in MongoDB?
The namespace (database.collection) in the message has no collection. Check the database you are connected to and the exact, case-sensitive collection name.
Why does find() return nothing but getIndexes() fails?
A query on a missing collection is answered with an empty result, not an error, while listIndexes needs an existing collection and returns 26. Both point to a wrong name.
Does dropping a missing collection give error 26?
The documentation says db.collection.drop() still returns true when the collection does not exist. Commands such as dropIndexes, collMod and renameCollection do return 26.
Learn more on CodeWithSQL
Official documentation
- MongoDB error codes (26 NamespaceNotFound)
- db.getCollectionNames()
- db.collection.drop()
- db.collection.renameCollection()
Last technically reviewed 24 October 2026; examples and diagnostics run on MongoDB 8.0 documentation and source (error_codes.yml r8.0.0); not run locally. Codes and code names are checked against MongoDB's error_codes.yml (8.0); no MongoDB server was available, so the examples were not run locally.