node.js 在 mongodb 中插入当前日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19612338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:05:45  来源:igfitidea点击:

Inserting the current datetime in mongodb

node.jsmongodbmongojs

提问by David

I have been having trouble inserting an actual datetime object in mongodb using the mongojs driver for nodejs. Any help?

我一直无法使用 nodejs 的 mongojs 驱动程序在 mongodb 中插入实际的日期时间对象。有什么帮助吗?

var currentdate = new Date(); 
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1)  + "/" 
+ currentdate.getFullYear() + " @ "  
+ currentdate.getHours() + ":"  
+ currentdate.getMinutes() + ":" 
+ currentdate.getSeconds();

db.test.update({
    conversation: conv
},{ 
    $push:{ messages: {
        message: message,
        pseudo: name,
        current_date: datetime
    }}
},{upsert: true});

回答by Salvador Dali

You do not need to do all this manual date creation.

您不需要进行所有这些手动日期创建。

db.test.update({
    conversation: conv
}, { 
    $push:{ messages: {
        message: message,
        pseudo: name,
        current_date: new Date()
    } }
}, {
    upsert: true
});

would do the job.

会做的工作。

Also keep in mind, that in Mongo 2.6 among many other features you can use $currentDatewhich might be handy.

还要记住,在 Mongo 2.6 和许多其他功能中,您可以使用$currentDate这可能很方便。