I'm trying to perform an aggregate query that uses the $dateToString aggregation function but whenever I execute it i get the following error:
can't convert from BSON type string to Date
Note that if I execute the query through any client the query perfectly answers with expected result: a counter for each day.
My code is the following:
getResultsDashboard: async function (req, res) {
var db = myTestCollection.getDatastore().manager;
var rawmyTestCollection = db.collection(myTestCollection.tableName);
let aggregateQuery = [
{
$match: { field1: "myValue" },
},
{
$group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
count: { $sum: 1 },
},
},
{
$sort: { _id: -1 },
},
];
let resultsByDate = await rawmyTestCollection.aggregate(aggregateQuery).toArray();
return res.json(resultsByDate);
}
I've also tried with native instead
myTestCollection.native((err, coll) => {
coll.aggregate(aggregateQuery).toArray((err2, result) => console.log(result));
});
But no results, only a message of deprecation for the native method.
I guess it is the driver for mongo. Is there any other driver for sails? Or how can I implement the query in other way? Except, of course, getting from the db all the dates and then performing all the aggregation in javascript.