'MongoDB Count By Current Month
I'm new to MongoDB. I need to get the count of the posts which are posted in this current month. PlateModel is my model I've kept timestamps true also. It looks like the below:
{ timestamps: true }
My present code in the Controller looks like below:
const today = new Date();
const host_count = await PlateModel.find({
$and: [
{
postedBy: req.user._id,
},
{
createdAt: today.getMonth(),
},
],
}).count();
But, I get the count value 0. Can anyone help me to figure out the problem?
Solution 1:[1]
You can use the Moment library.
const moment = require("moment");
const firstdate = moment().startOf('month').format('DD-MM-YYYY');
console.log(firstdate);
const lastdate=moment().endOf('month').format("DD-MM-YYYY");
console.log(lastdate);
const host_count = await PlateModel.find({
$and: [
{
postedBy: req.user._id,
},
{
createdAt: {
$gte:firstdate,
$lt:lastdate
}
},
],
}).count();
Solution 2:[2]
Try this:
df.apply(
lambda x: df[
(df.event_b < x.event_creation) &
(df.event_id < x.event_id) &
(df.user == x.user)
].USD.sum(),
axis=1)
You can also use groupby instead of df.user == x.user
If you have a very large dataset it might become slow, since you are slicing the df for each row.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sai krishna |
| Solution 2 | Paul |
