Javascript 向mongodb js添加时间戳

Javascript 向mongodb js添加时间戳,javascript,mongodb,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'); va

寻找添加时间戳的最佳方法是什么? 查找格式: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);
        }
    });
});
});

谢谢

只需添加到您的邮件对象

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

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

您可以使用,这是MongoDB的最佳ORM,还支持createdAt和updatedAt功能。

获得我的答案,更改突出显示

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);
        }
    });
});
});
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时区=-1//9? '': '0');
_时间戳+=字符串(分钟);
_时间戳+=':';
_时间戳+=(秒>9?“”:“0”);
_时间戳+=字符串(秒);
_时间戳+=':';
_时间戳+=字符串(毫秒);

//谢谢@sesamechick打印以下内容:{“_id”:ObjectId(“5a8472a54d7063cb19a9eff9”),“主题”:“用户/60019466E0F2/温度”,“消息”:“71”,“时间戳”:1518629541232}
contd=新日期(1518629541232);//返回2018年2月14日星期三17:03:09 GMT-0500(美国东部时间)
再次在客户端解析此日期。