无法运行“shardCollection”等命令或“启用分片”
我继承了一个需要维护的项目。显然,我们决定从 CosmosDb 切换到专用 MongoDb。 CI 管道中有一个项目,它使用集合为 mongo 数据库提供种子并创建索引。现在它是用这样的命令完成的:
var partition = new Dictionary<string, object> {
{"shardCollection", $"{databaseName}.{collectionName}"},
{ "key", new Dictionary<string, object>() { { $"{indexName}", "hashed" } } }
};
var command = new BsonDocumentCommand<BsonDocument>(partition);
var response = adminDb.RunCommand(command);
这在我的机器上不起作用 - 我收到 Command shardCollection failed: no such command 'shardCollection'.
任何想法我做错了吗?我在其他地方找到了该代码的示例作为工作代码,我很困惑。
更新 1
在尝试对我的集合设置分片之前,我也尝试运行此命令:
var shardDbResult = adminDatabase.RunCommand<MongoDB.Bson.BsonDocument>(
new MongoDB.Bson.BsonDocument() {
{ "enableSharding", $"{databaseName}" }
});
但我也收到类似的错误 Command enableSharding failed: no such command 'enableSharding'
。我什至尝试过 mongosh
:
更新 2
所以最终我认为这可能与我的 MongoDb 安装有关。所以我决定在docker中启动一个分片副本集,显然我成功地运行了。 现在命令:
db.adminCommand({enableSharding: "<databaseName>"})
工作正常(至少它打印报告:ok: 1
)。此命令也工作正常:
db.adminCommand({shardCollection: "<databaseName>.<collectionName>", key: {<fieldName>: 1}})
但问题顶部的我的代码连续失败并显示相同的消息...
版本:
- 操作系统 Windows 10 Enterprise 21H2
- .net 5 (5.0.15)
- MongoDb 5.0
- Mongo.Bson nuget 2.15.0
- Mongo。驱动程序 nuget 2.15.0
- Mongo.DriverCore nuget 2.15.0
I inherited a project to maintain. Apparently there was a decision to switch from CosmosDb to dedicated MongoDb. There is a project in a CI pipeline that seeds mongo database with collections and creates indexes. Now it is done with commands like that:
var partition = new Dictionary<string, object> {
{"shardCollection", quot;{databaseName}.{collectionName}"},
{ "key", new Dictionary<string, object>() { { quot;{indexName}", "hashed" } } }
};
var command = new BsonDocumentCommand<BsonDocument>(partition);
var response = adminDb.RunCommand(command);
This does not work on my machine - I am getting Command shardCollection failes: no such command 'shardCollection'.
Any ideas what am I doing wrong? I found examples of that code elsewhere as a working code and I am very confused.
Update 1
I also tried to run this command before trying to set sharding on my collections:
var shardDbResult = adminDatabase.RunCommand<MongoDB.Bson.BsonDocument>(
new MongoDB.Bson.BsonDocument() {
{ "enableSharding", quot;{databaseName}" }
});
but I also get a similar error Command enableSharding failes: no such command 'enableSharding'
. I even tried that from mongosh
:
Update 2
So eventually I figured this might be something with my MongoDb installation. So I decided to start a sharded replica set in docker, which apparently I managed to run.
Now the command:
db.adminCommand({enableSharding: "<databaseName>"})
works fine (at least it prints the report: ok: 1
). Also this command works fine:
db.adminCommand({shardCollection: "<databaseName>.<collectionName>", key: {<fieldName>: 1}})
But my code from the top of the question continuously fails with the same message...
Versions:
- OS Windows 10 Enterprise 21H2
- .net 5 (5.0.15)
- MongoDb 5.0
- Mongo.Bson nuget 2.15.0
- Mongo.Driver nuget 2.15.0
- Mongo.DriverCore nuget 2.15.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的分片集群副本集完全错误。一旦我使用了 这个 stackoverflow 答案 中部署 mongodb 分片集群的说明,我的代码就开始工作了。
I had a totally wrong set my replica set with sharding cluster. Once I used the instructions for deploying mongodb sharded cluster from this stackoverflow answer, my code started working.