Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:16:08+00:00 2026-06-16T04:16:08+00:00

I have a small web server written using node.js, express and serial port which

  • 0

I have a small web server written using node.js, express and serial port which would constantly listen to an temperature sensor attached to the mac via USB. The code is as follow:

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

As it can be seen from the above code, my sensor values are stored in “data”.

Now I would like to save this data into my tempsensor collection which has the following format:

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

My question is:

1: How can I save the “data” in the value object, using the mongodb driver for node.js?
2: How can I add the time when the data is added automatically?

I know there’s a function called new Date() for the date, is there a similar function for time?

I would really appreciate any help.

Thanks a ton in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:16:09+00:00Added an answer on June 16, 2026 at 4:16 am

    You can do something like this to insert your document into your collection.

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am thinking of making an (initially) small Web Application, which would eventually have
I have a small console app containing a web server written in c#. When
I have written a small custom web server application in C running on Linux.
I have a small self-written web-server, that is capable of processing POST\GET queries. Also,
I have a small web application in asp.net and I'm using SQL Server. How
I have a web application written using CherryPy, which is run locally on 127.0.0.1:4321
I have an embedded glassfish server and a small web app. I want to
I have defined small MXML component which calls web service which returns random number
I'm running into a (hopefully) small issue. I have a web app written in
I have a small Python web application using the Cherrypy framework. I am by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.