The goal that I have is to implement a php class (through the MongoDB/Driver object) to handle the db connection, insert and truncate.
This is the class prepared (today):
class mongodb {
private $mongo;
/*
* Constructor to instantiat db connection
*/
function __construct($host) {
$this->db = new MongoDB\Driver\Manager("mongodb://" . $host . "/");
}
/*
* Delete collection (upon $filter parameter);
*/
function delete($collection, $filter) {
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete($filter);
$result = $this->db->executeBulkWrite($collection, $bulk);
}
/*
* Insert dataset
*/
function insert($collection, $dataset) {
$bulk = new MongoDB\Driver\BulkWrite;
if (!is_array($dataset)) $dataset = (array) $dataset;
foreach($dataset as $k => $ds) {
$i = $bulk->insert($ds);
}
$result = $this->db->executeBulkWrite($collection, $bulk);
}
/*
* Insert js fx into db.system.js collection
*/
function insertfx($dataset) {
$bulk = new MongoDB\Driver\BulkWrite;
$i = $bulk->insert($dataset);
$result = $this->db->executeBulkWrite("db.system.js", $bulk);
}
}
If I run this code I don’t have any error…
$mongodb = new MongoDb("localhost:27017");
$dataset = [
[
'_id' => 23120378,
'x_origin' => -80,
'y_origin' => 101,
'hard_bin_map' => 'FFFFAAAAFFFF|FFAA0101AAFF'
],[
'_id' => 23120379,
'x_origin' => -80,
'y_origin' => 101,
'hard_bin_map' => 'FFFFAAAAFFFF|FFAA0203AAFF'
]
];
/*
* truncate & fill collection st.map
*/
$mongodb->delete("st.map", []);
$mongodb->insert("st.map", $dataset);
/*
* clean + insert a js function into db.system.js
*/
$mongodb->delete("db.system.js", []);
$mongodb->insertfx(array(
'_id' => "map2matrix",
'value' => "function (inputmap, startx, starty, splitsize) {
inputmap = inputmap.split(',');
let y = starty;
matrix = []
inputmap.forEach((el) => {
let columns = Math.ceil(el.length / splitsize);
matrix[y] = [];
x = startx;
for (i = 0, o = 0; i < columns; ++i, o += splitsize) {
matrix[y][x] = el.substr(o, splitsize);
x++;
}
y++;
});
return(matrix);
}"
));
but:
a) I can query st.map collection (through MongoDB Compass)
b) I cannot see db.system.js containing ‘map2matrix’
The goal, at the end (and I would appreciate if someone can support me on how to reach this goal) is to trigger fx map2matrix() passing as input
-
st.map.hard_bin_map (ie:
FFFFAAAAFFFF|FFAA0203AAFF
) - st.map.x_origin (ie: 101)
- st.map.y_origin (ie: -80)
- 2 (hardcoded split length)
The expected output has to be a matrix like this
| -80 -79 -78 -77 -76 -75
----+-----------------------------------------------
101 | 'FF' 'FF' 'AA' 'AA' 'FF' 'FF'
102 | 'FF' 'AA' '01' '01' 'AA' 'FF'
Is it correct that way that I use to store data and function?
How can I call fx map2matrix passing values stored on st.map collection?
type or paste code here