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});
MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.
Making sure that all your applications create and update MongoDB timestamps is going to be the hardest part of this process; MongoDB has no automatic timestamp support, so every one of your applications is going to have to make sure they do one of two things when working with the database: write a createdAt field, or ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With