javascript - Moment converts date as string for find query and object for aggregate query. How & Why? -
when use find query returns date object whereas while using aggregate gives date string after converting using moment. why?
find query
this.find({},{ "updated_at":1, "created_at":1, },callback);
aggregate query
this.aggregate([{ $project:{ "updated_at" :1, "created_at" :1, } }],callback);
now when convert date est using moment following results
// lets query result in dataarray var created_at = moment(dataarray[0].created_at); var created_at_est = created_at.clone().tz("america/new_york"); dataarray[0].created_at = created_at_est.format('ddd mmm dd yyyy hh:mm:ss'); console.log(typeof(dataarray[0].created_at)); // result string in aggregate query , object in find query
i not able understand why happening? can explain this?
it's because find
provides results mongoose document instances (which can't freely modified) while aggregate
provides results plain objects (which can).
so if want modify created_at
field in find
results, tell mongoose provide plain objects instead chaining call lean()
:
this.find({},{ "updated_at":1, "created_at":1, }).lean().exec(callback);
Comments
Post a Comment