Definition
- db.collection.getShardLocation()
- New in version 8.1. - Returns a document containing the shards where the collection is located and whether the collection is sharded. - Important- mongosh Method- This page documents a - mongoshmethod. This is not the documentation for database commands or language-specific drivers.- The - getShardLocation()method is a wrapper for the- $listClusterCatalogaggregation stage.
Output
The getShardLocation() method returns a document with the following
fields:
| Field | Type | Description | 
|---|---|---|
| 
 | Array | Shards where the collection's data resides. | 
| 
 | Boolean | Indicates whether the collection is sharded. | 
If you run the method on an unsharded deployment:
- The - shardsarray is empty.
- The - shardedfield is- false.
Compatibility
This method is available in deployments hosted in the following environments:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud 
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
- MongoDB Enterprise: The subscription-based, self-managed version of MongoDB 
- MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB 
Syntax
db.<collection>.getShardLocation() 
Examples
Sharded Collection
The following example shows the shards that contain the data in the
sample_mflix.movies collection:
use sample_mflix db.movies.getShardLocation() 
{ shards: [ 'shard01', 'shard02' ], sharded: true } 
Unsharded Collection on a Sharded Cluster
If you run the command on a sharded cluster but the collection is not
sharded, the sharded field is false and the shards array
only contains the config shard:
use sample_mflix db.movies.getShardLocation() 
{ shards: [ 'config' ], sharded: false } 
Unsharded Deployment
If you run the command on an unsharded deployment, the sharded field
is false and the shards array is empty:
use sample_mflix db.movies.getShardLocation() 
{ shards: [], sharded: false }