Вставка текущего времени в mongodb
У меня возникли проблемы с вставкой фактического объекта datetime в mongodb с помощью драйвера mongojs для nodejs. Любая помощь?
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});
Ответы
Ответ 1
Вам не нужно делать все это вручную.
db.test.update({
conversation: conv
}, {
$push:{ messages: {
message: message,
pseudo: name,
current_date: new Date()
} }
}, {
upsert: true
});
выполнит эту работу.
Также имейте в виду, что в Mongo 2.6 среди многих других функций вы можете использовать $currentDate, что может быть удобно.