Database Reference
In-Depth Information
Interesting! We have
5,073
item counts in the replica set. If we get the same query from
a standalone
mongod
instance, you can see the rest of the data in this shard as shown in
the following screenshot:
Well, the sharding feature distributes the entire dataset into two separate shards using our
hash-based shard key.
Using the same method, you can define other shard keys, and MongoDB will use those
shard keys to split data.
The following is another example of adding a shard key. In this example, we use shard
keys
x
and
y
on collection
test2
. The command for this shard key is like the following:
sh.shardCollection("newdb.test2", { "x": 1, "y": 1 })
The result of issuing the preceding command is demonstrated in the following screenshot:
Using this key, MongoDB splits data with the
x
field, and then if the collection has the
same data on the
x
key, it will use the
y
field to split the data.
This screenshot illustrates the output of the
sh.status()
command after adding a new
collection to a shard: