How to Fix MongoDB Error 118 (NamespaceNotSharded)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 118 when you run a sharding command on an unsharded collection. You can fix this error by sharding the collection first or removing the sharding command. The database engine labels this issue as NamespaceNotSharded. A namespace in MongoDB combines your database name and your collection name. When a query or admin task expects distributed data, the cluster checks the namespace status. If that collection lives on one shard without a shard key, the command stops right away. T

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You see mongodb error 118 when you run a sharding command on an unsharded collection. You can fix this error by sharding the collection first or removing the sharding command.

The database engine labels this issue as NamespaceNotSharded. A namespace in MongoDB combines your database name and your collection name. When a query or admin task expects distributed data, the cluster checks the namespace status. If that collection lives on one shard without a shard key, the command stops right away. This safeguard prevents broken splits, bad chunk moves, and failed balancing tasks across your nodes.

What causes mongodb error 118 in your cluster

Your team will hit mongodb error 118 during cluster maintenance or deployment scripts. It happens when an admin runs chunk commands against a normal collection. The cluster router checks the config database to find the shard key for that namespace. When the router finds no sharding record for the collection, it returns code 118.

Automated migration scripts often trigger this failure in staging or production. A script might try to split chunks before the collection sharding step finishes. Developers also see this when they copy scripts from a sharded setup to a single replica set. Even on a sharded cluster, each collection starts unsharded until you set it up.

Typos in the database or collection name also trigger this specific code. If an engineer types the wrong namespace, the router looks up a blank record. Because the mistyped collection is not sharded, the engine returns NamespaceNotSharded instead of a missing table error. You should always check the exact spelling of both the database and the collection first.

How to diagnose and fix the failure in production

To find the root cause, start with read-only checks on your cluster metadata. You do not need full admin rights to check which collections use sharding. MongoDB grants access through roles that define actions on specific resources down to the collection level. The built-in read role lets you list collections, list indexes, and view collection stats safely. Using a read role stops engineers from making accidental data changes during an incident.

Check the collection stats to see if the namespace has a shard key defined. Also verify that the parent database has sharding turned on in the cluster. If the collection should remain unsharded, take the chunk command out of your app or script. This simple change fixes the error without changing how your cluster stores data.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If the collection needs to scale across shards, plan the change with care. First, verify that the index for your planned shard key exists on the collection. A user with the readWrite role can create indexes and collections when needed. Next, have a database admin enable sharding on the namespace during a low-traffic window. Keep role permissions tight so routine app accounts cannot run cluster admin actions.

Preventing sharding errors with safer database workflows

Many sharding failures happen when engineers run manual commands in production terminals. When every developer has broad admin roles, one wrong namespace name can stall a deployment. You can prevent these mistakes by separating daily read tasks from cluster change tasks. Give engineers the basic read role for day-to-day checks and debugging work.

When someone needs to run a sharding script, require a quick peer review first. Checking the target database name and collection status takes only a minute. You should also protect sensitive customer fields during these debugging sessions. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This approach keeps your production data safe while your team fixes cluster routing issues.

Inspect MongoDB wire traffic in flight

Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.

First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.

Frequently asked questions

What does NamespaceNotSharded mean in MongoDB?

NamespaceNotSharded is the official name for error code 118 in MongoDB. It means a command tried to run a sharded operation on a collection that is not sharded.

Can a typo in my query trigger mongodb error 118?

Yes, a typo in the database name or collection name often causes this error. The cluster checks the wrong name, sees it is not sharded, and blocks the command.

What role do I need to check collection stats safely?

You only need the built-in read role on the target database to check stats. This role allows collection stats and index checks without allowing data writes or drops.

The query runs. The emails stay hidden.