2012-12-20 145 views
0

我有一個使用node.js編寫的小型web服務器,它可以不斷地通過USB監聽連接到mac的溫度傳感器。代碼如下:通過node.js將文檔插入到mongodb數據庫集合中

var serialport = require("serialport"),  // include the serialport library 
    SerialPort = serialport.SerialPort,  // make a local instance of serial 
    app = require('express')(),   // start Express framework 
    server = require('http').createServer(app), // start an HTTP server 
    io = require('socket.io').listen(server); // filter the server using socket.io 

var mongo = require('mongodb'); 

var Server = mongo.Server, 
    Db = mongo.Db, 
    BSON = mongo.BSONPure; 

var server = new Server('localhost', 27017, {auto_reconnect: true}); 
db = new Db('sensordatabase', server); 

db.open(function(err, db) { 
    if(!err) { 
    console.log("Connected to 'sensordatabase' database"); 
    db.collection('tempsensor', {safe:true}, function(err, collection) { 
     if (err) { 
     console.log("The 'values' collection doesn't exist. Creating it with sample data..."); 
     } 
    }); 
    } 
}); 

var serialData = {};    // object to hold what goes out to the client 
server.listen(8080);    // listen for incoming requests on the server 
console.log("Listening for new clients on port 8080"); 

// open the serial port. Change the name to the name of your port, just like in Processing and Arduino: 
var myPort = new SerialPort("/dev/cu.usbmodem5d11", { 
// look for return and newline at the end of each data packet: 
    parser: serialport.parsers.readline("\r\n") 
}); 

// respond to web GET requests with the index.html page: 
app.get('/', function (request, response) { 
    myPort.on('data', function (data) { 
    // set the value property of scores to the serial string: 
    serialData.value = data; 
    response.send(data); 
    // for debugging, you should see this in Terminal: 
    console.log(data); 
    }); 
}); 

從上面的代碼可以看出,我的傳感器值存儲在「數據」中。

現在我想將這些數據保存到我的tempsensor收集其格式如下:

{ 
    "Physicalentity": "Temperature", 
    "Unit": "Celsius", 
    "value": "", 
    "time": "", 
    "date": "" 
    }, 

我的問題是:

1:我怎樣才能保存「數據」的價值對象,使用node.js的mongodb驅動程序? 2:如何添加數據自動添加的時間?

我知道日期中有一個叫做new Date()的函數,對於時間有沒有類似的函數?

我真的很感激任何幫助。

感謝提前一噸。

回答

1

你可以這樣做,將文檔插入到你的集合中。

db.collection('tempsensor',{safe:true}, function(err, collection) { 

collection.insert({ 
"Physicalentity": "Temperature", 
"Unit": "Celsius", 
"value": "", 
"time": "", 
"date": "" 
}, function(err, doc) { 
    if(err){ 
    console.log("Error on document insert"); 
    }else{ 
    //Document saved succesfuly 
    } 
    }); 
}); 
+0

我將如何將變量數據插入對象值? – Spaniard89

1
> db.c.insert({'date': new Date(), 'time_hours': new Date().getHours(), 'time_mi 
n': new Date().getMinutes()}) 
Inserted 1 record(s) in 31ms 
> db.c.find({}) 
{ "_id" : ObjectId("50d30884059dc377c6ff66ec"), "date" : ISODate("2012-12-20T12: 
45:56.493Z"), "time_hours" : 16, "time_min" : 45 } 
Fetched 1 record(s) in 0ms 
> 

使用蒙戈外殼。這意味着你可以在任何mongo驅動程序中使用它。

1

請記住 - 這不是學習教程的地方,這是人們有技術或軟件問題與他們的學習能力無關的地方。爲了改善這一點,請閱讀使用mongodb的示例並全面使用node.js。

這是在你的情況的一些細節來引導你:

在回調函數上myPort.on(「數據」),您可以訪問你的數據,這就是你要救精確到位你的數據到數據庫。

與此同時,當您初始化數據庫連接和集合時,您需要獲取集合的句柄以便在應用程序中使用它之後。 在db.collection('tempsensor')的回調函數中,你有對象集合 - 這是你需要執行mongodb函數來處理該集合中的數據。

因此,將此變量保存在共享作用域的某處(可以是全局變量或集合數組)。

然後在收到的數據回調中,使用此收集並傳遞Serdar Dogruyol建議的數據。

相關問題