如何使用PHP从mongoDB中的文档中获取字段?

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?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐