dongyan1808 2019-04-29 22:59
浏览 82

如何使用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?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型