user3577947
user3577947

Reputation: 287

How to check whether the collection is sharded or not in Mongodb

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

Answers (6)

prasad_
prasad_

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

Mohammad Jawad Barati
Mohammad Jawad Barati

Reputation: 754

Mongoose

I have written these two helper function to verify whether a database/collection is partitioned or not.

NOTES:

  1. db refers to the targeted database name.
  2. My assumption is that you are connecting to your 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

Check whether a database is sharded or not.

  1. Connect to one of the shards' mongosh, in my case it is a docker container docker exec -it mongo-shard2-1 mongosh.
  2. use config
  3. db.databases.find({ _id: "db_name" })
  4. In the returned JSON there is a field called partitioned that is the answer to your question.

Check whether a collection is sharded or not.

  1. Connect to one of the shards' mongosh, in my case it is a docker container docker exec -it mongo-shard1-1 mongosh.
  2. use config
  3. db.collections.find({ _id: "db_name.collection_name" })
  4. If it returned a document you can be sure that your collection is sharded for sure.

Upvotes: 0

gimalay
gimalay

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

shivam chowdhary
shivam chowdhary

Reputation: 11

db.printShardingStatus()

gives metadata also

Upvotes: 0

MrRobot
MrRobot

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

Boris
Boris

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

Related Questions