简体   繁体   English

在Mongodb中存储实时数据

[英]storing live data in Mongodb

I am pushing data to a server using an API from a website. 我正在使用网站上的API将数据推送到服务器。 Every time my arduino detects a pulse it sends it to COSM. 每当我的arduino检测到一个脉冲时,就会将其发送到COSM。 And by using a trigger, the data goes to my server. 通过使用触发器,数据将发送到我的服务器。 I was initially writing the data into a .txt file and a Json object, but now I want to start implementing mongo to have different collections. 我最初是将数据写入.txt文件和Json对象,但现在我想开始实现mongo以具有不同的集合。

For some reason the data is not being transmitted to my server after I added the Mongo connection when I tried to write it as a Mongo collection. 由于某种原因,当我尝试将其写为Mongo集合时,添加Mongo连接后,数据未传输至服务器。 I want to be able to write down the information in Mongo directly and avoid creating files. 我希望能够直接在Mongo中写下信息并避免创建文件。

Any suggestion is more than welcome, here is the code: 任何建议都值得欢迎,下面是代码:

<?php

// Creating a data base in Mongodb to store my information from my $jsonFile
$connection = new MongoClient(); //Connect to mongo
$db = $connection -> qandm; // select my DB which is called qandM
$collection = $db -> pulseData;

//Gets all the information from Cosm
$pulse = $_POST['body'];

//access and open a file
//echo $pulse;

//Converts the data into PHP arrays
$pulseObj = json_decode($pulse, true);

//Parse through the specific information from the array and gets each piece of information in an array
$userName = $pulseObj["triggering_datastream"]["id"];
$dataTime= $pulseObj["triggering_datastream"]["at"];
$dataValue= $pulseObj["triggering_datastream"]["value"]["value"];


//Writes all the data coming from COSM
$file = fopen("data.txt", "a+");//a+ give the option to have the cursor at the end to access the file read and write it
    /*  $pulse.="\r\n"; */
fwrite($file, $pulse);//takes incoming data and writes it in the file
fclose($file);

//Opens a new .txt file and writes the values that we selected before into our file
$string = $userName." ".$dataTime." ".$dataValue." \r\n";
//error_log allows me to see in my Apache log server the information that I'm printing
error_log($string);

//Write all the information I parsed in my three variables in a new file
$file2 = fopen("rawData.txt", "a+");
fwrite($file2,$string);
fclose($file2);

//json sample

//Inputs the data from the time and the pulse value into a json object
$json = array("User" => $userName, "timestamp"=> $dataTime, "value"=> $dataValue);

//Opens a new json object
$jsonFile = fopen("data.json", "a+");

//Writes the data of our new arrayed information into the open json object
fwrite($jsonFile, json_encode($json));
fclose($jsonFile);




//A loop to populate
foreach($json as $data){
    $collection->insert($data);

}

//find the data I just stored
$cursor = $collection->find();

//Output it in a UL
echo "<p> My Pulse </p>";

echo '<ul>';
foreach($cursor as $doc){

    echo' <li> My pulse is: '.$doc['value'];

}
 echo '</ul>';


/*$data = "data.txt";
$fh = fopen($data, "w") or die ("can't open file");
$data = json_encode($_POST);
fwrite($fh, $data);
fclose($fh);*/

//print_r($file);



?>

This is likely the source of your problems: 这可能是您出现问题的根源:

//A loop to populate
foreach($json as $data){
    $collection->insert($data);

}

You are iterating over your $json array and passing the values (but not the keys) to the insert method of MongoCollection. 您正在遍历$ json数组,并将值(但不是键)传递给MongoCollection的insert方法。 According to the doc this method expects an object or an array. 根据文档,此方法需要一个对象或数组。 Based on what I understand your code to be trying to do this foreach loop should be replaced with the following: 根据我的理解,您要尝试执行此foreach循环的代码应替换为以下内容:

$collection->insert($json);

This will create a mongo object resembling your array. 这将创建一个类似于您的数组的mongo对象。 Your code currently is attempting to insert the values of each array element as an individual entry. 您的代码当前正在尝试将每个数组元素的值作为一个单独的条目插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM