Database Reference
In-Depth Information
to shard). This collection serves as the main data table for a fictitious website called TextAndARandomNumber.com .
The following code creates a PHP program that inserts data into your sharded server:
<?php
// Open a database connection to the mongos daemon
$mongo = new MongoClient("localhost:27021");
// Select the test database
$db = $mongo->selectDB('testdb');
// Select the TestIndex collection
$collection = $db->testcollection;
for($i=0; $i < 100000 ; $i++){
$data=array();
$data['testkey'] = rand(1,100000);
$data['testtext'] = "Because of the nature of MongoDB, many of the more "
. "traditional functions that a DB Administrator "
. "would perform are not required. Creating new databases, "
. "collections and new fields on the server are no longer necessary, "
. "as MongoDB will create these elements on-the-fly as you access them."
. "Therefore, for the vast majority of cases managing databases and "
. "schemas is not required.";
$collection->insert($data);
}
This small program will connect to the shard controller ( mongos ) and insert 100,000 records with random
testkeys and some testtext to pad out the documents. As mentioned previously, this sample text causes these
documents to occupy a sufficient number of chunks to make using the sharding mechanism feasible.
The following command runs the test program:
$php testshard.php
Once the program has finished running, you can connect to the mongos instance with the command shell and
verify that the data has been stored:
$mongo localhost:27021
>use testdb
>db.testcollection.count()
100000
At this point, you can see that your server has stored 100,000 records. Now you need to connect to each shard and
see how many items have been stored in testdb.testcollection for each shard. The following code enables you to
connect to the first shard and see how many records are stored in it from the testcollection collection:
$mongo localhost:27023
>use testdb
>db.testcollection.count()
48875
Search WWH ::




Custom Search