在mongodb中插入当前日期时间 [英] Inserting the current datetime in mongodb

查看:1599
本文介绍了在mongodb中插入当前日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法使用用于nodejs的mongojs驱动程序在mongodb中插入实际的datetime对象.有什么帮助吗?

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

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

推荐答案

您无需完成所有手动创建日期的操作.

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

会做这份工作.

还请记住,在Mongo 2.6的许多其他功能中,您可以使用 $ currentDate 可能很方便.

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

这篇关于在mongodb中插入当前日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆