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.

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大