What it means
A namespace (“ns”) is a database and a collection together: shop.orders. Some commands work on an
existing collection and fail when there isn’t one, with code 26, NamespaceNotFound. The message
varies with the command, and usually names the namespace:
| Command | Message on MongoDB 8.0 |
|---|---|
dropIndex, dropIndexes | ns not found seo_err_mongo.missing |
getIndexes (listIndexes) | ns does not exist: seo_err_mongo.missing |
collMod | ns does not exist |
renameCollection | Source collection seo_err_mongo.missing does not exist |
validate | Collection 'seo_err_mongo.missing' does not exist to validate. |
stats(), $collStats | … Collection [seo_err_mongo.missing] not found. |
Ordinary reads and writes don’t raise it: a find on a missing collection returns nothing, and an
insert creates the collection. So the error points at admin work: indexes, renames, schema changes,
statistics.
Before MongoDB 7.0, the drop command also failed with ns not found for a missing collection
(some drivers and shells hid that by returning false); since 7.0 it succeeds.
Common causes
- The wrong database. The connection string’s path (or
use) picks a different database from the one holding the collection, for exampletest, the default when the URL has none. - A misspelt or wrongly cased name. Names are case-sensitive:
Ordersandordersare two collections. Mongoose also pluralises and lowercases model names (Userbecomesusers). - The collection doesn’t exist yet: a migration or test runs
dropIndex,collModor a rename before anything has written to it. - It was already dropped or renamed, by an earlier run of the same script or by another process.
- Old servers and drivers: test teardown that drops collections that were never created fails on servers before 7.0.
How to fix it
Check where you are
db.getName()
db.getCollectionNames()
On the command line, mongosh "<connection string>" --eval 'db.getName()' shows which database the
URL selects. If the collection is elsewhere, put that database in the connection string’s path, or
use db.getSiblingDB("<database>").
Find the collection by name
To search every database you can see for a name, ignoring case:
db.adminCommand({ listDatabases: 1, nameOnly: true }).databases.forEach(d =>
db.getSiblingDB(d.name).getCollectionNames()
.filter(c => c.toLowerCase() === "orders")
.forEach(c => print(`${d.name}.${c}`))
)
Create it first, or skip when it’s missing
In scripts and migrations, check before you act:
if (db.getCollectionNames().includes("orders")) {
db.orders.dropIndex("placedAt_1")
}
Or create the collection explicitly with db.createCollection("orders") before collMod or
index changes. In application code, catch code 26 where a missing collection is fine (for example
in test teardown) and let other errors through.
Use the name Mongoose uses
Model.collection.collectionName prints the real name. To choose it yourself, pass it as the third
argument: mongoose.model("User", schema, "User").
Reproduce it
MongoDB 8.0.32, mongosh 2.12.0, in a database with no collection missing:
db.missing.dropIndex("a_1")
MongoServerError[NamespaceNotFound]: ns not found seo_err_mongo.missing
The error’s code was 26. The other messages in the table came from getIndexes(), collMod,
renameCollection("other"), validate() and stats(). reIndex() said collection does not exist. Meanwhile db.missing.drop() returned true, find() returned nothing and
countDocuments() returned 0, all without an error.
In Inlet
Inlet shows MongoDB collections as tables, so you can see which collections a database holds and
how their names are spelt. Queries use mongosh syntax, so db.getName(), db.getCollectionNames()
and the index commands above run in a query tab.