adding timestamp to mongodb js

lostislandguy Source

Looking to add timestamp what is the best method? looking for format: 2-14-2018 12:08 not sure if adding pm/am is possible?

current collection print:

{ "_id" : ObjectId("5a845c0abbf804a41878bbcb"), "topic" : 
"user/60019466E0F2/temperature", "message" : "71" }

current .js code

var mongodb  = require('mongodb');
var mqtt     = require('mqtt');
var config   = require('./config');

var mqttUri  = 'mqtt://' + config.mqtt.hostname + ':' + config.mqtt.port;
var client   = mqtt.connect(mqttUri);

client.on('connect', function () {
client.subscribe(config.mqtt.namespace);
});

var mongoUri = 'mongodb://' + config.mongodb.hostname + ':' + 
config.mongodb.port + '/' + config.mongodb.database;
mongodb.MongoClient.connect(mongoUri, function(error, database) {
if(error != null) {
    throw error;
}

var collection = database.collection(config.mongodb.collection);
collection.createIndex( { "topic" : 1 } );

client.on('message', function (topic, message) {
    var messageObject = {
        topic: topic,
        message: message.toString()
    };

    collection.insert(messageObject, function(error, result) {
        if(error != null) {
            console.log("ERROR: " + error);
        }
    });
});
});

Thank you

javascriptmongodb

Answers

answered 3 months ago sesamechicken #1

Just add to your message object

var messageObject = {
    topic: topic,
    message: message.toString(),
    timestamp: Date.now()
};

If this format isn't acceptable, parse this client-side.

answered 3 months ago Narek Hakobyan #2

you can use Mongoose, this is the best ORM for MongoDB, also supports createdAt and updatedAt functionality

answered 3 months ago lostislandguy #3

got my answer, changed highlighted..

var mongodb  = require('mongodb');
var mqtt     = require('mqtt');
var config   = require('./config');

var mqttUri  = 'mqtt://' + config.mqtt.hostname + ':' + config.mqtt.port;
var client   = mqtt.connect(mqttUri);

**var timezone = -1;    //<--- define timezone here**

client.on('connect', function () {
client.subscribe(config.mqtt.namespace);
});

var mongoUri = 'mongodb://' + config.mongodb.hostname + ':' + 
config.mongodb.port + '/' + config.mongodb.database;
mongodb.MongoClient.connect(mongoUri, function(error, database) {
if(error != null) {
    throw error;
}

var collection = database.collection(config.mongodb.collection);
collection.createIndex( { "topic" : 1 } );

client.on('message', function (topic, message) {
    **//Added Manux
    var _now    = new Date();
    //timezone
    var now     = new Date(_now.getTime() + (timezone * 3600 * 1000));
    var day     = now.getDate();     //<-- get day of month
    var month   = now.getMonth() + 1;    //<-- get month of year
    var year    = now.getFullYear(); //<-- get year
    var hour    = now.getHours();
    var minute  = now.getMinutes();
    var second  = now.getSeconds();
    var millis  = now.getMilliseconds();

    var _timestamp = (day>9? '': '0');
        _timestamp += String(day);
        _timestamp += '/';
        _timestamp += (month>9? '': '0');
        _timestamp += String(month);
        _timestamp += '/';
        _timestamp += String(year);
        _timestamp += ' ';
        _timestamp += (hour>9? '': '0');
        _timestamp += String(hour);
        _timestamp += ':';
        _timestamp += (minute>9? '': '0');
        _timestamp += String(minute);
        _timestamp += ':';
        _timestamp += (second>9? '': '0');
        _timestamp += String(second);
        _timestamp += ':';
        _timestamp += String(millis);

    //<-- end Manux**

    var messageObject = {
        topic: topic,
        message: message.toString()
    };

    **var messageObject = {
        topic: topic,
        message: message.toString(),
        timestamp: _timestamp**
 };


    collection.insert(messageObject, function(error, result) {
        if(error != null) {
            console.log("ERROR: " + error);
        }
    });
});
});

comments powered by Disqus