MongoDB: 在分片集群上查询

MongoDB: Querying on a sharded cluster

我有一个名为 test2 的分片集群。

db.collection.getShardDistribution() 给我这个:

为什么我的 .count() 只查询 shard0000 中的 return 总文档,而我的 iterable 只遍历 shard0000 中的文档?我需要做什么才能使我的查询 return 整个集合?

    System.out.println("Total docs: "+db.getCollection(collectionName).count());
    FindIterable<Document> iterable = db.getCollection(collectionName).find();

我使用不同参数执行的任何查询只会遍历 shard0000。

编辑 连接字符串:

MongoClient mongoClient = new MongoClient("129.241.xxx.xx",27017);

这是错误。应该是端口 27023。谢谢!

问题是数据库分片后我连接到错误的端口。我必须修改我的连接字符串以使用正确的端口,其中我的 mongo 实例是 运行.

这是我的连接字符串。

MongoClient mongoClient = new MongoClient("129.241.xxx.xx",27017);

将端口更改为

MongoClient mongoClient = new MongoClient("129.241.xxx.xx",27023);

...它奏效了。感谢@Markus W Mahlberg 为我指明了正确的方向。