【问题标题】:storing live data in Mongodb在MongoDB中存储实时数据
【发布时间】:2013-04-06 23:12:24
【问题描述】:

我正在使用来自网站的 API 将数据推送到服务器。每次我的 arduino 检测到脉冲时,它都会将其发送到 COSM。通过使用触发器,数据会发送到我的服务器。我最初将数据写入 .txt 文件和 Json 对象,但现在我想开始实现 mongo 以拥有不同的集合。

由于某种原因,当我尝试将 Mongo 连接编写为 Mongo 集合时,数据未传输到我的服务器。我希望能够直接在 Mongo 中写下信息,避免创建文件。

欢迎提出任何建议,代码如下:

<?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);



?>

【问题讨论】:

    标签: php mongodb arduino database


    【解决方案1】:

    这可能是您的问题的根源:

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

    您正在遍历 $json 数组并将值(但不是键)传递给 MongoCollection 的插入方法。根据doc,此方法需要一个对象或一个数组。根据我对您的代码的理解,您尝试执行此 foreach 循环的代码应替换为以下内容:

    $collection->insert($json);
    

    这将创建一个类似于您的数组的 mongo 对象。您的代码当前正尝试将每个数组元素的值作为单独的条目插入。

    【讨论】:

    • 山姆,非常感谢您的回复,我犯了个小错误。出于某种原因,我想使用 foreach() 为来自我的脉冲传感器的每个传入字符串分配一个唯一的 _id,它发送 3 个值。但这很好用!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2011-05-15
    相关资源
    最近更新 更多