2012-12-20 148 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建议的数据。

相关问题