Open ansh opened 3 years ago
After some debugging, I was able to fix the raw query that was in the logs before forest-express-mongoose failed.
order.aggregate(
[
{$match: {created_on: {$ne: null}}},
{
$group: {
_id: {
year: {$year: [{$subtract: ['$created_on', -18000000]}]},
month: {$month: [{$subtract: ['$created_on', -18000000]}]},
day: {$dayOfMonth: [{$subtract: ['$created_on', -18000000]}]},
},
created_on: {$first: '$created_on'},
count: {$sum: 1},
},
},
{$sort: {created_on: 1}},
{$project: {values: {key: '$_id', value: '$count'}}},
],
{},
);
db.order.aggregate(
[
{$match: {created_on: {$ne: null}}},
{
$group: {
_id: {
year: {$year: [{$toDate: {$subtract: ['$created_on', -18000000]}}]},
month: {$month: [{$toDate: {$subtract: ['$created_on', -18000000]}}]},
day: {$dayOfMonth: [{$toDate: {$subtract: ['$created_on', -18000000]}}]},
},
created_on: {$first: '$created_on'},
count: {$sum: 1},
},
},
{$sort: {created_on: 1}},
{$project: {values: {key: '$_id', value: '$count'}}},
],
{},
);
Clearly, the fix was adding $toDate
in front of $year
, $month
, and $dayOfMonth
. This worked because all 3 of those commands take in only a Date object according to MongoDB. Therefore, we needed to convert our Int64 Epoch time to a Date before passing it into the query.
The question still remains: What is the problem in the Source Code and how to fix it?
Expected behavior
In our case, we use Unix (Epoch) time stored as an Int64 on MongoDB for all our Date/Time fields like created_on, updated_on, etc. We expect these fields to be converted to Date objects by fores-express-mongoose. These Date objects should then be easily searchable, sort-able, and just generally something we can manipulate. Furthermore, when using these fields (created_on, updated_on) in the Dashboard view to create charts, it should work normally and function correctly.
Actual behavior
What actually happens is that we see some errors when using the fields created_on and updated_on when trying to create Charts in the Dashboard view. This error says
[forest] 🌳🌳🌳 Unexpected error: can't convert from BSON type long to Date MongoError: can't convert from BSON type long to Date
.Failure Logs
Context
TODO: Please provide any relevant information about your setup.