Reputation: 539
How to use MongoDB Aggregation Pipeline Operators using Mongoose in nodejs
I am trying to count datewise data using aggregate
Walkins.aggregate([
{
$match:{'subscriberId': subsc._id},
},{
$project:{'date':{ $convert: { input:'$created', to: "date"} },
} },{
$project:{ 'dates': { $dateToString : { format: "%Y-%m-%d", date: "$created" } }} ,
},{
$group:{
_id:{'date': '$created'} ,
walkinsCount :{$sum: 1 }
}
}
]).then((walkins)=>{ });
But it is troughing "MongoError: Unrecognized expression '$convert'" Error
Walkins.aggregate([
{
$match:{'subscriberId': subsc._id},
},{
$project:{'date':{ $toDate:'$created'} },
} },{
$project:{ 'dates': { $dateToString : { format: "%Y-%m-%d", date: "$created" } }} ,
},{
$group:{
_id:{'date': '$created'} ,
walkinsCount :{$sum: 1 }
}
}
]).then((walkins)=>{ });
Upvotes: 2
Views: 2799
Reputation: 21
$toDate
is only available in version 4.0
and above. Here, you can see it's written New in version 4.0.
which means you can't use it with a MongoDB installation of anything lower than version 4.0
.
If you're using Ubuntu and installed MongoDB using apt install mongodb
, it installs version 3.6.3
.
Check out the official documentation on how to install and run the latest version here for your OS.
Upvotes: 2