将时间戳添加到mongodb js

时间:2018-02-14 17:13:51

标签: javascript mongodb

想要添加时间戳,最好的方法是什么? 寻找格式:2-14-2018 12:08不确定是否可以添加pm / am?

当前收藏打印:

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

当前.js代码

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

谢谢

3 个答案:

答案 0 :(得分:0)

只需添加到您的讯息对象

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

如果此格式不可接受,请解析此客户端。

答案 1 :(得分:0)

你可以使用Mongoose,这是MongoDB最好的ORM,也支持createdAt和updatedAt功能

答案 2 :(得分:0)

得到了我的回答,突出显示..

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