Reputation: 287
I Wrote
db.runCommand({enablesharding : "database"})
db.runCommand({shardcollection : "database.coll_1",key : {_id :"hashed"}})
And how to check is collection sahrded or not? Maybe exist some function that I do not know. Plese help me!
Upvotes: 11
Views: 15977
Reputation: 14287
After sharding a collection, you can use the sh.status()
(or sh.printShardingStatus()
) to see that your collection is sharded. This command is run from mongos
router. It doesn't return a JSON, but a printout of sharding information from the config
database (this database has collections which support sharding operations). In the output, look for the section databases - Sharded Collections).
The config
database has collections and can be queried to know the sharding information needed. One of the collections name is collections
. This has information about all the collections that are sharded.
From a mongos
router you can query the following:
> use config
> show collections
> db.collections.find()
This should show all the database collections that are sharded and their shard key data: config.collections. To query specific sharded collection:
> db.collections.find( { _id: "database_name.collection_name" } )
Upvotes: 0
Reputation: 754
I have written these two helper function to verify whether a database/collection is partitioned or not.
NOTES:
db
refers to the targeted database name.mongos
router/s. E.g. mongodb://localhost:27100,localhost:27200
.async function isDatabaseSharded(
connection: Connection,
db: string,
): Promise<boolean> {
const result = await connection
.useDb('config')
.collection('databases')
.findOne({ _id: db as any });
if (!result) {
return false;
}
return result.partitioned;
}
async function isCollectionSharded(
connection: Connection,
db: string,
collection: string,
): Promise<boolean> {
const result = await connection
.useDb('config')
.collection('collections')
.findOne({ _id: `${db}.${collection}` as any });
return result !== null;
}
mongosh
, in my case it is a docker container docker exec -it mongo-shard2-1 mongosh
.use config
db.databases.find({ _id: "db_name" })
partitioned
that is the answer to your question.mongosh
, in my case it is a docker container docker exec -it mongo-shard1-1 mongosh
.use config
db.collections.find({ _id: "db_name.collection_name" })
Upvotes: 0
Reputation: 493
You can run this to list all not sharded collections
db.getCollectionInfos().forEach(function(c) { if(!db.getCollection(c.name).stats().sharded) { print(c.name)} })
or this to print sharded ones
db.getCollectionInfos().forEach(function(c) { if(!db.getCollection(c.name).stats().sharded) { print(c.name)} })
Upvotes: 0
Reputation: 501
I Use the following :
use dbname
db.colname.getShardDistribution()
Outputs the sharding distribution if it's sharded, and Collection dbname.colname is not sharded
if it's not.
Check this for more details.
I also use @Scott's comment aswell (use databaseName; db.collectionName.stats().sharded
) but i prefer the getShardDistribution
, it's cleaner.
Upvotes: 18
Reputation: 162
connect to mongos and run the following:
use config;
db.collections.find( {_id: "db.colname" , dropped : false } )
if collection is shared it'll appear there along with it sharding key
Upvotes: 2