I wrote this code:
<?php
// Creating Connection
$mng = new MongoDB\Driver\Manager(
'mongodb+srv://removedUsername:removedPassword@cluster0-vqdka.mongodb.net/test?retryWrites=true');
$menuName = $_POST['menuName'];
$itemName = $_POST['itemName'];
$itemPrice = $_POST['itemPrice'];
$addedIcon = $_POST['addedIcon'];
$smallPrintDetails = $_POST['smallPrintDetails'];
$addedSize = $_POST['addedSize'];
$bulk = new MongoDB\Driver\BulkWrite;
$doc = ["menuID" => new MongoDB\BSON\ObjectID,
"menuName" => $menuName,
"itemName" => $itemName,
"itemPrice" => $itemPrice,
"addedIcon" => $addedIcon,
"smallPrintDetails" => $smallPrintDetails,
"addedSize" => $addedSize];
$bulk->insert($doc);
$mng->executeBulkWrite('restaurantApp.menus', $bulk);//end of good.
$bulk2 = new MongoDB\Driver\BulkWrite;
foreach($_POST['selectRestaurant'] as $currentRestaurant) {
echo $currentRestaurant;
$bulk2->update(['newRestaurantName' => $currentRestaurant], ['$set' => ['MenusAttached' => $menuName]]);
}
$mng->executeBulkWrite('restaurantApp.restaurants', $bulk2);
?>
I put the entire code for this part for clarity for others viewing this later. MongoDB PHP docs aren't that good.
We are focusing on after where the comment "end of good" is.
What this part does is it loops through all the restaurants selected by the user in the select box. so FOR EACH restaurant selected by the user, it's going to go to the database, to that exact restaurant and add the same variable $menuName to all of them to the field MenusAttached.
Now if you focus where this line is:
$bulk2->update(['newRestaurantName' => $currentRestaurant], ['$set' => ['MenusAttached' => $menuName]]);
I am trying to append $menuName to the field MenusAttached, but right now what it's doing as you can see is just replacing it. The php .= does not work here.
So I figured the solution would be FOR EACH RESTAURANT to grab the value of the field MenusAttached, store it as a variable, and go from there.
Anyone know?