Skip to content

Commit 8fc0138

Browse files
committed
From Object to Array
1 parent 0a7ae4c commit 8fc0138

File tree

1 file changed

+174
-25
lines changed

1 file changed

+174
-25
lines changed

mass-update.php

+174-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?php
2-
$time1 = microtime(true);
2+
ob_implicit_flush(true);
3+
ob_end_flush();
4+
5+
echo ('PHP: ');
6+
7+
$time1 = '';
8+
$time2 = microtime(true);
39
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
410

511
require_once('dbconn.php');
6-
$bulk = new MongoDB\Driver\BulkWrite();
12+
713

814
$mapping = 'mass_upload';
915

1016
// this must be called after the $mapping variable is declared
11-
get_the_job_done();
17+
//$output = get_the_job_done();
18+
//insert_log($output);
1219

20+
$output = populate_array();
21+
insert_log($output);
1322

1423
// Mapping for ...
1524
function mass_upload($doc){
@@ -30,11 +39,15 @@ function mass_upload($doc){
3039
'onhand_value.BR19' => (double)(number_format($doc->avg_cost->BR19 * $doc->onhand->BR19,2,'.','')),
3140
'onhand_value.BR21' => (double)(number_format($doc->avg_cost->BR21 * $doc->onhand->BR21,2,'.','')),
3241
'onhand_value.BR22' => (double)(number_format($doc->avg_cost->BR22 * $doc->onhand->BR22,2,'.','')),
33-
'onhand_value.BRPT' => (double)(number_format($doc->avg_cost->BRPT * $doc->onhand->BRPT,2,'.',''))
42+
'onhand_value.BRPT' => (double)(number_format($doc->avg_cost->BRPT * $doc->onhand->BRPT,2,'.','')),
43+
'onhand_value.BR60' => (double)(number_format($doc->avg_cost->BR60 * $doc->onhand->BR60,2,'.',''))
3444
];
3545
return $map;
3646
}
47+
3748
/*
49+
To run this query in MongoDB Shell, use this command:
50+
3851
db.products3.find({pline: {$ne:"NONSTOCK-"}, pline: {$exists:1}, onhand: {$exists:1}, avg_cost: {$exists:1} } ).forEach(function(doc) {
3952
db.products3.update({ _id: doc._id }, { $set: {
4053
"onhand_value.BR01": parseFloat((doc.onhand.BR01 * doc.avg_cost.BR01).toFixed(2)),
@@ -52,55 +65,191 @@ function mass_upload($doc){
5265
"onhand_value.BR19": parseFloat((doc.onhand.BR19 * doc.avg_cost.BR19).toFixed(2)),
5366
"onhand_value.BR21": parseFloat((doc.onhand.BR21 * doc.avg_cost.BR21).toFixed(2)),
5467
"onhand_value.BR22": parseFloat((doc.onhand.BR22 * doc.avg_cost.BR22).toFixed(2)),
55-
"onhand_value.BRPT": parseFloat((doc.onhand.BRPT * doc.avg_cost.BRPT).toFixed(2))
56-
68+
"onhand_value.BRPT": parseFloat((doc.onhand.BRPT * doc.avg_cost.BRPT).toFixed(2)),
69+
"onhand_value.BR60": parseFloat((doc.onhand.BR60 * doc.avg_cost.BR60).toFixed(2))
5770
} } )
5871
})
5972
*/
6073

6174
function get_the_job_done(){
62-
global $manager, $bulk, $mapping;
75+
global $manager, $mapping, $time1;
76+
$time1 = microtime(true);
77+
$bulk = new MongoDB\Driver\BulkWrite();
6378
$query = new MongoDB\Driver\Query(
64-
[
65-
//'pline' => ['$ne' => 'NONSTOCK'],
79+
[ // query (empty: select all)
80+
//'sales_all' => ['$gt' => 100], // ADDED FOR TESTINTG
6681
'onhand' => ['$exists' => true],
6782
'avg_cost' => ['$exists' => true]
68-
], // query (empty: select all)
69-
[
83+
],
84+
[ // options
7085
'projection' => ['avg_cost' => 1, 'onhand' => 1]
7186
//'limit' => 10
7287
//'sort' => [ 'onhand.BR01' => -1], 'skip' => 0, 'limit' => 1
73-
] // options
88+
]
7489
);
7590

7691
// Execute query and obtain cursor:
7792
$cursor = $manager->executeQuery('onlinestore.products3', $query );
78-
79-
// Display info in a table
80-
//echo '<table class="fixed_headers seven_columns">';
81-
//echo "<thead><tr><th>Product ID & Buyline</th><th>Product Description</th><th>$ / Qty: </th><th>onhand</th><th>avg cost</th></tr></thead>\n";
93+
$j = 0; $k = 0;
8294
foreach ($cursor as $doc) {
83-
84-
//echo '<tr><td><a href="product/'.$doc->_id .'/">'.$doc->_id . '</a> ('.$doc->pline. ')</td>'.
85-
//'<td>--</td><td>$' . ''.
86-
//"</td><td>".$doc->onhand->BR01."</td><td>". $doc->avg_cost->BR01 . "</td></tr>\n";
87-
//echo $doc->_id . "<br />\n";
88-
8995
$bulk->update(['_id' => intval($doc->_id)], ['$set' => $mapping($doc)], ['multi' => false, 'upsert' => true]);
96+
$j++;
97+
if($j>10000){
98+
$k++;
99+
echo ','.$k;
100+
$j=0;
101+
}
90102
}
91-
//echo "</table>";
92103

93104
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
94105
$result = $manager->executeBulkWrite('onlinestore.products3', $bulk, $writeConcern);
95106

96107
// display results on one line
97-
printf("PHP matched %d, ", $result->getMatchedCount());
108+
printf("\nmatched %d, ", $result->getMatchedCount());
109+
printf("inserted %d, ", $result->getInsertedCount());
110+
printf("updated %d, ", $result->getModifiedCount());
111+
printf("upserted %d.\n", $result->getUpsertedCount());
112+
113+
$output = (object) [
114+
'matched' => $result->getMatchedCount(),
115+
'inserted' => $result->getInsertedCount(),
116+
'modified' => $result->getModifiedCount(),
117+
'upserted' => $result->getUpsertedCount()
118+
];
119+
120+
return $output;
121+
}
122+
123+
// insert_log updates log collection
124+
function insert_log($log){
125+
global $manager, $time1;
126+
$bulk = new MongoDB\Driver\BulkWrite();
127+
128+
// creating document to insert into log collection
129+
$_GET['file'] = 'Mass Load';
130+
$doc = [
131+
'date' => new MongoDB\BSON\UTCDateTime,
132+
'execution_time' => round( ( (microtime(true) - $time1)),3 ),
133+
'file_name' => $_GET['file'],
134+
'matched' => $log->matched,
135+
'inserted' => $log->inserted,
136+
'updated' => $log->modified,
137+
'upserted' => $log->upserted,
138+
'ip' => $_SERVER['REMOTE_ADDR'],
139+
'browser' => $_SERVER['HTTP_USER_AGENT'],
140+
'page' => $_SERVER['REQUEST_URI']
141+
];
142+
143+
// inserting document into log collection
144+
$bulk = new MongoDB\Driver\BulkWrite();
145+
$bulk->insert($doc);
146+
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
147+
$result = $manager->executeBulkWrite('onlinestore.insert_log', $bulk, $writeConcern);
148+
}
149+
150+
151+
152+
// Mapping for populate_array()
153+
function elements($line){
154+
$map = [
155+
"branch" => ['$each' => $line]
156+
];
157+
return $map;
158+
}
159+
/* To run this query in MongoDB Shell, use this command:
160+
It works with testing mechanisms.
161+
function isNumber(n) {
162+
return !isNaN(parseFloat(n)) && isFinite(n);
163+
}
164+
db.people.find( { "data" : { $type : 3 } } ).forEach( function (x) {
165+
var out =[];
166+
for( var i in x.data ) {
167+
if (x.data.hasOwnProperty(i)){
168+
if (isNumber(i)){
169+
out[i] = x.data[i];
170+
}else{
171+
out.push(x.data[i]);
172+
}
173+
}
174+
}
175+
x.data = out
176+
db.people.save(x);
177+
});
178+
*/
179+
180+
// This function get data from each product as object and saves as an array.
181+
function populate_array(){
182+
global $manager, $mapping, $time1;
183+
$time1 = microtime(true);
184+
$bulk = new MongoDB\Driver\BulkWrite();
185+
$query = new MongoDB\Driver\Query(
186+
[
187+
//'_id' => 1,
188+
'sales' => ['$exists' => 1]
189+
],
190+
[ // options
191+
'projection' => ['sales' => 1]
192+
//'limit' => 10
193+
//'sort' => [ 'onhand.BR01' => -1], 'skip' => 0, 'limit' => 1
194+
]
195+
);
196+
197+
// Execute query and obtain cursor:
198+
$cursor = $manager->executeQuery('onlinestore.products3', $query );
199+
$j = 0; $k = 0;
200+
echo ' Array: ';
201+
foreach ($cursor as $doc) {
202+
$arr = toArray($doc->sales);
203+
//$arr = $arr['sales'];
204+
//print_r($arr);
205+
$bulk->update(['_id' => intval($doc->_id)], ['$addToSet' => elements($arr)], ['multi' => false, 'upsert' => false]);
206+
//echo 'out: ' . $doc->_id ."<br>\n";
207+
$j++;
208+
if($j>10000){
209+
$k++;
210+
echo ','.$k;
211+
$j=0;
212+
}
213+
}
214+
215+
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
216+
$result = $manager->executeBulkWrite('onlinestore.products3', $bulk, $writeConcern);
217+
218+
// display results on one line
219+
printf("\nmatched %d, ", $result->getMatchedCount());
98220
printf("inserted %d, ", $result->getInsertedCount());
99221
printf("updated %d, ", $result->getModifiedCount());
100222
printf("upserted %d.\n", $result->getUpsertedCount());
223+
224+
$output = (object) [
225+
'matched' => $result->getMatchedCount(),
226+
'inserted' => $result->getInsertedCount(),
227+
'modified' => $result->getModifiedCount(),
228+
'upserted' => $result->getUpsertedCount()
229+
];
230+
231+
return $output;
232+
}
233+
234+
// toArray is special helper function to convert object to an array.
235+
function toArray($obj){
236+
if (is_object($obj)) $obj = (array)$obj;
237+
if (is_array($obj)) {
238+
$new = array();
239+
$i = 0;
240+
foreach ($obj as $key => $val) {
241+
$subArray['id'] = $key;
242+
$subArray['sales'] = toArray($val);
243+
$new[$i] = $subArray;
244+
$i++;
245+
}
246+
} else {
247+
$new = $obj;
248+
}
249+
return $new;
101250
}
102251

103252

104253
// show how much time it took to process
105-
echo 'PHP script execution time: ' .round( ( (microtime(true) - $time1)),3 ). ' milliseconds';
254+
echo 'PHP time: ' .round( ( (microtime(true) - $time2)),3 ). ' seconds';
106255
?>

0 commit comments

Comments
 (0)