dongsigan2044 2019-02-27 12:40
浏览 82
已采纳

编写一个php文件,将文档添加到mongoDB集合中

Below is my recordSigfox.php file. For now I am testing it from the addess bar: http://...server.../recordSigfox.php?data=e049d3c02b7f5542

Tested separately and working: -The first part connects to mongoDB and creates a testData collection. -The second part decodes the hex message into its original components, a latitude and a longitude. I WANT TO RECORD THESE in testData. -The third part just lists the contents of testData.

Apparent issue code:

  $document = array(
          "Latitude" => $lt,
          "Longitude" => $lg,
        );    
        $manager->$collection->insert($document);

Full code:

<html>
  <head>
   <title>Unpack</title>
  </head>
  <body>
<?php
    //connect to DB
    $user = "muntean";
    require '/var/composer/vendor/autoload.php';
    $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    echo "Connection to database successfully";
    $collection = new MongoDB\Collection($manager, $user, 'testData');

    //get data from the address bar: e049d3c02b7f5542
    $_data = $_GET["data"];
    $coord = unpack('flat/flon', pack('H*', $_data));
    $lt = $coord['lat'];
    $lg = $coord['lon'];
    //echo lt shows: -6.6027679443359
    //echo lg shows: 53.374187469482

    $document = array(
      "Latitude" => $lt,
      "Longitude" => $lg,
    );    
    $manager->$collection->insert($document);

    //shows contect of testData when not empty
    $filter = [];
    $options = [];
    $query = new MongoDB\Driver\Query($filter, $options);
    $cursor = $manager->executeQuery("$user.testData", $query);
    print("The contents of the collection $user.testData are:");
    print_r($cursor->toArray());
?>
  </body>
</html>
  • 写回答

1条回答

  • douyaosi3164 2019-02-27 13:49
    关注

    Fixed.

    Got it to work with:

    try {
          $collection->insertOne($document);
       } 
    catch (\Exception $e) {
          print("Insert failed.");
          print_r($document);
          print_r($e);
          exit();
        }
    

    I did this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?