Database Reference
In-Depth Information
for (var i = 1; i <= 10000; i++) {
db.test.insert({
data: i
});
}
You can simply run the preceding code in the
mongo
shell, and as a result, you have
10,000
, as data on the collection
test
. This is shown in the following screenshot:
After adding data to the collection, you can check other shards to see data on them. The
following screenshot shows the
mongo
shell for the
rs1
replica set. This is shown in the
following screenshot:
As you can see, we created the database and collection on the
mongos
instance, but we
have data on other shards too.
Because we have two shards at the moment, MongoDB splits data into two shards. So, if
we run the following command to get the data count in a replica set, we will see fewer
counts than what we inserted using the JavaScript code (
10,000
items). The following
code depicts this:
db.test.find().count()
The following screenshot shows the output of the preceding command: