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

下面是我的recordSigfox.php文件。 现在我正在从addess栏进行测试:
http://。 ..server ... / recordSigfox.php?data = e049d3c02b7f5542 </ p>

单独测试并正常工作:
-第一部分连接到mongoDB并创建一个testData集合。

-第二部分将十六进制消息解码为其原始组件,即纬度和经度。 我想在testData中记录这些内容。
-第三部分只列出了testData的内容。</ p>

表观问题代码:</ p>

   $ document = array(
“Latitude”=&gt; $ lt,
“经度”=&gt; $ lg,
);

$ manager-&gt; $ collection-&gt; insert($ document);
</ code> </ pre>

完整代码:</ p>

 <  code>&lt; html&gt; 
&lt; head&gt;
&lt; title&gt;解包&lt; / title&gt;
&lt; / head&gt;
&lt; body&gt;
&lt;?php
//连接到DB
$ user =“muntean”;
require'/var/composer/vendor/autoload.php';
$ manager = new MongoDB \ Driver \ Manager(“mongodb:// localhost:27017”);
echo“ 成功连接数据库“;
$ collection = new MongoDB \ Collection($ manager,$ user,'testData');

//从地址栏获取数据:e049d3c02b7f5542
$ _data = $ _GET [” data“];
$ coord = unpack('flat / flon',pack('H *',$ _data));
$ lt = $ coord ['lat'];
$ lg = $ coord [ 'lon'];
// echo lt shows:-6.6027679443359
// echo lg显示:53.374187469482

$ document = array(
“Latitude”=&gt; $ lt,
“经度”= &gt; $ lg,
);

$ manager-&gt; $ collection-&gt; insert($ document);

//显示testData在非空时的情况
$ filter = [];
$ options = [];
$ query = new MongoDB \ Driver \ Query($ filter,$ options);
$ cursor = $ manager-&gt; executeQuery(“$ user.testData”,$ query);
print(“集合的内容$ user.testData是:“);
print_r($ cursor-&gt; toArray());
?&gt;
&lt; / body&gt;
&lt; / html&gt;
</ code> </ pre>

</ div>

展开原文

原文

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>

douxu5233
douxu5233 我想将lt和lg记录为我的集合testData中的文档。所以它可能看起来像:。。。纬度:-6.6027679443359经度:53.374187469482
一年多之前 回复
dongzhijing8202
dongzhijing8202 你的问题不明确。请尝试改写它。
一年多之前 回复

1个回答



已修复。</ p>

可以使用它:</ p>

< pre> try {
$ collection-&gt; insertOne($ document);
}
catch(\ Exception $ e){
print(“Insert failed。”);
print_r($ document) ;
print_r($ e);
exit();
}
</ code> </ pre>

我这样做了。</ p>
</ div>

展开原文

原文

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.

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