2013-10-26 41 views
14

我一直在使用mongojs驱动程序为nodejs在mongodb中插入实际的日期时间对象时遇到问题。任何帮助?在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}); 
+5

只需插入'new Date()'? – Sammaye

回答

26

您不需要执行所有此手动创建日期。

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

会做这项工作。

另外请记住,在Mongo 2.6的许多其他功能中,你可以使用$currentDate这可能会很方便。

相关问题