我需要检索有关 MongoDB 集合的分片配置的信息,特别是
shardKey
和 unique
字段。虽然我可以在 MongoDB shell 中使用 sh.status()
命令轻松获取此信息,但我一直无法弄清楚如何使用 MongoDB Node.js 驱动程序执行此操作。
是否可以使用 Node.js 驱动程序检索此信息,如果可以,我该怎么做?
我已经尝试过 collection.stats(),但没有成功。返回的对象值中有一个
shards
对象,但我需要的字段不存在。
const client = await MongoClient.connect(uri);
const db = client.db(dbName);
const stats = await db.collection(collectionName).stats();
我不太确定您需要什么信息,您可以使用
adminCommand
在 nodejs 中运行
runCommand
等效项
例如
db.runCommand({ listShards: 1 })
你可以为分片运行的命令列表是here.
看看
db.getSiblingDB("config").getCollection("collections").find()
提供分片键。要获得唯一字段,您需要查询索引。会是这样的:
db.collection.getIndexes()
你也可以看看收藏
config.database
,config.shards
和,config.chunks