2013-10-26 58 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這可能會很方便。

相關問題