• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

How to fix MongoDB Error Code – 118 – NamespaceNotSharded

January 2, 2024

How to Fix MongoDB Error Code – 118 – NamespaceNotSharded

MongoDB is a popular NoSQL database that offers high performance, scalability, and flexibility. However, like any software, it can encounter errors that need to be resolved. One such error is Error Code – 118 – NamespaceNotSharded. In this article, we will explore what this error means and how to fix it.

Understanding Error Code – 118 – NamespaceNotSharded

When working with MongoDB, sharding is a technique used to distribute data across multiple machines or shards. It allows for horizontal scaling and improved performance. The NamespaceNotSharded error occurs when a query is executed on a non-sharded collection or namespace that should be sharded.

By default, MongoDB does not shard collections automatically. You need to explicitly enable sharding for the desired collections or namespaces. If you attempt to run a query on a non-sharded collection that should be sharded, MongoDB will throw the Error Code – 118 – NamespaceNotSharded.

Fixing Error Code – 118 – NamespaceNotSharded

To fix the NamespaceNotSharded error, you need to enable sharding for the appropriate collection or namespace. Follow these steps:

Step 1: Connect to MongoDB

Open a MongoDB client, such as the MongoDB shell, and connect to your MongoDB instance.

$ mongo

Step 2: Enable Sharding

Before enabling sharding for a collection, you need to enable sharding for the database that contains the collection. Use the following command to enable sharding for the desired database:

sh.enableSharding("your_database_name")

Step 3: Shard the Collection

Once sharding is enabled for the database, you can shard the specific collection. Use the following command to shard the collection:

sh.shardCollection("your_database_name.your_collection_name", {"shard_key": "hashed"})

Replace “your_database_name” with the name of your database and “your_collection_name” with the name of your collection. The “shard_key” should be replaced with the appropriate field that will be used as the shard key.

For example, if you have a collection named “users” in the “mydb” database, and you want to shard it based on the “username” field, the command would be:

sh.shardCollection("mydb.users", {"username": "hashed"})

Step 4: Verify Sharding

After executing the shardCollection command, you can verify if the collection is successfully sharded by running the following command:

sh.status()

This command will display information about the sharded collections in your MongoDB instance.

Summary

In conclusion, the Error Code – 118 – NamespaceNotSharded in MongoDB occurs when a query is executed on a non-sharded collection that should be sharded. To fix this error, you need to enable sharding for the appropriate collection or namespace. By following the steps outlined in this article, you can successfully resolve the NamespaceNotSharded error and ensure proper sharding in your MongoDB deployment.

For more information about VPS hosting and how it can benefit your MongoDB deployment, consider exploring Server.HK. With their top-notch VPS solutions, you can achieve optimal performance and scalability for your MongoDB databases.

Recent Posts

  • How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  • CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  • Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  • Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026
  • Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot