I'm coding a testing program: 200 processes write 10000 records to one collection in mongodb The record should be appended to the end of the collection, so I follow the example [push new value to mongodb inner array][1] - mongodb/php
below is my code:
$data = $memcache->get('data');
$m = new MongoClient();
$db = $m->record_test;
$collection = $db->record;
$time = $db->record_time;
for ($i=0; $i < 10000; $i++) {
$time_start = microtime_float();
$collection->insert($data);
$time_end = microtime_float();
$run_time = $time_end - $time_start;
$newdata = array('$push' => array('time' => $run_time));
$time->update(array("time" => 0 ), $newdata);
}
$data's content: Array ( [0] => Array ( [Register_Name] => MGC_IP [Register_Value] => 192.168.1.9 [Register_Unit] => [Device_Name] => this [R_W] => )
[1] => Array
(
[Register_Name] => MGC_SW_VERSION
[Register_Value] => 0.9.15.1022_SPI
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[2] => Array
(
[Register_Name] => MGC_FW_VERSION
[Register_Value] => 0.1.15.915.1
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[3] => Array
(
[Register_Name] => MGC_ZONE_ID
[Register_Value] => zone0
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[4] => Array
(
[Register_Name] => MGC_DEVICE_ID
[Register_Value] => mgc0
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[5] => Array
(
[Register_Name] => MGC_SYS_TIME
[Register_Value] => 2015/10/27 10:12:04
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[6] => Array
(
[Register_Name] => MGC_P1_STATUS
[Register_Value] => Access fails
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
[7] => Array
(
[Register_Name] => MGC_P2_STATUS
[Register_Value] => Not configured
[Register_Unit] =>
[Device_Name] => this
[R_W] => R
)
)
After 10000 records has be done, I got nothing in the collection! I used this sample code to check my result:
$m = new MongoClient();
echo "Connection to database successfully.<br>";
$db = $m->record_test;
echo "Database record_test selected.<br>";
$time = $db->record_time;
echo "Collection record selected succsessfully.<br>";
echo $time->count(), "
";
$cursor = $time->find();
var_dump(iterator_to_array($cursor));
Output:
Connection to database successfully.
Database record_test selected.
Collection record selected succsessfully.
0
Could someone help me? Why my var_dump() nothing? I'm really new in mongodb and PHP :'(