2013-10-26 20 views
14

mongojsドライバを使ってnodejsに実際のdatetimeオブジェクトを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を使用すると便利かもしれないことに注意してください。

関連する問題