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

would do the job.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top