How to Fix MongoDB Error 298 (InvalidServerType)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 298 when a node receives a command meant for another server type. To fix this InvalidServerType error, point your client to the right cluster node or router. A MongoDB cluster uses several node types to store and route data. Each node type runs a strict list of allowed commands. When an app or admin sends a query to the wrong node, the engine stops the request. It then returns code 298 with the InvalidServerType label. Root causes of mongodb error 298 in a cluster Mongo

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 get mongodb error 298 when a node receives a command meant for another server type. To fix this InvalidServerType error, point your client to the right cluster node or router.

A MongoDB cluster uses several node types to store and route data. Each node type runs a strict list of allowed commands. When an app or admin sends a query to the wrong node, the engine stops the request. It then returns code 298 with the InvalidServerType label.

Root causes of mongodb error 298 in a cluster

MongoDB splits cluster work across shard servers, config servers, and query routers. A standalone database server also behaves in its own way. Each server type only accepts commands built for its specific job. If you send a shard router command to a standalone server, it fails.

Bad connection strings cause most of these errors in production. An app might connect straight to a replica set member instead of a query router. An engineer might also run a cluster admin script against a single node by mistake. Network routing changes or stale DNS records can also send traffic to the wrong host.

Cluster state changes can trigger this error code during maintenance too. A node might still be starting up or leaving a replica set. While in that state, its server type does not match what the client expects. The database blocks the action so it can protect data integrity across the cluster.

How to check and fix InvalidServerType in production

Start by checking which host and port received the failed command. Look at your app connection settings and compare them against your cluster topology map. Make sure app traffic goes through the query router in a sharded setup. Only send admin commands to the exact node type that supports them.

When you check the cluster, do not use full admin accounts for simple checks. MongoDB grants access through roles that define allowed actions on specific resources. A role applies to its defined database and can control access down to the collection level. Use the built-in read role when you only need to inspect non-system collections. The read role allows safe inspection actions like finding records and viewing collection stats. It does not allow actions that change or drop 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 you must fix data or indexes after fixing the routing, use the readWrite role. It adds actions to insert, update, remove, and manage indexes on that database. Note that MongoDB Atlas users have different built-in roles than self-hosted users. Still, both setups build their roles from the same set of privilege actions.

Preventing server type mismatches during admin work

Human mistakes often happen when engineers connect to individual cluster nodes by hand. A developer might open a direct shell session to a shard node to debug a bug. If they run a router command there, the server throws code 298 right away. Worse, direct node access makes it easy to run write commands on the wrong host.

You can stop these errors by standardizing how your team reaches the database. Set up fixed routing paths for read checks, cluster admin work, and app traffic. Keep engineers from typing raw node addresses into their local database tools. Also, apply data masking on query results to rewrite emails, phone numbers, and card numbers. This keeps sensitive records safe while engineers check server state and fix routing bugs.

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 MongoDB error code 298 mean?

Code 298 is the InvalidServerType error in MongoDB. It means you ran a command on a server role that does not support it.

Can a wrong connection string cause InvalidServerType?

Yes, pointing a client to a direct shard or config node instead of a router causes this. Fix the host list in your connection settings to point to the right server type.

Which role should I use to inspect a node safely?

Assign the built-in read role on the database you need to check. It lets you view collections and stats without the risk of changing or dropping data.

The query runs. The emails stay hidden.