These are chat archives for Automattic/mongoose

13th
Apr 2018
Bharath Kumar P
@pkbharath
Apr 13 2018 07:13
how to handle timezone in mongoosejs
its always storing in UTC which needs to be GMT +5:30
any help on this
Kev
@lineus
Apr 13 2018 07:30
@pkbharath how are you creating the value that gets stored in your Date path?
Bharath Kumar P
@pkbharath
Apr 13 2018 07:35
angular pushes the body using $resource and then mongoosejs stores the date in the backend
using datepicker
Kev
@lineus
Apr 13 2018 07:37
in your schema, is your date path schemaType Date?
Bharath Kumar P
@pkbharath
Apr 13 2018 07:37
yes
Kev
@lineus
Apr 13 2018 08:02
@pkbharath mongodb stores dates as BSON UTC datetime 64bit integer. docs here. It's unaware of timezone. You'll need to convert to whatever timezone you need, either manually or with a package like moment-timezone.
given that I'm currently on EDT, the manual creation of my current time would be Date.now() - (4 * 60 * 60 * 1000);
for India it would be Date.now() + (5 * 60 * 60 * 1000) + (30 * 60 * 1000);
Kev
@lineus
Apr 13 2018 08:07
which could probably be simplified, but I'm too tired for math :)
if you store it as the utc value you get from javascript Date(), mongodb aggregation has an operator for converting to a timezone with a given offset, but using aggregation pipelines for every query will be more of a hassle than just converting the date on the way in or the way out.
Alternatively, you can add a getter or a setter on the date path in your schema that mutates the date however you want before or after it gets saved in the db.
Kev
@lineus
Apr 13 2018 08:16
@pkbharath here is an example gist of a setter on a date path that automatically converts the UTC output of the javascript Date.now() method into GMT + 5:30.
Bharath Kumar P
@pkbharath
Apr 13 2018 08:59
@lineus Ty :smile:
Kev
@lineus
Apr 13 2018 09:00
happy to help!
Joe Early
@JoeEarly
Apr 13 2018 16:14
Hey all, am trying to return "ALL" from my db using mongoose. I have findONe working but find it not returning for me correctly I think
//Find One
module.exports.getProductByCategory = function (category, callback) {
    var query = {category: category};
    Product.findOne(query, callback);
};

//Find All
module.exports.getAllProductListings = function (callback) {
    var query = {};
    Product.find(query, function (err, callback) {

        console.log(callback);
    });


};
It logs to the console all right, but is not returning to the callback var at the start of my fucntion.