У меня есть коллекция user_status, и ее схема выглядит следующим образом
const userStatus = mongoose.model(
'user_status',
new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'user'
},
isActive: {
type: Boolean,
default: false
}
},
{ timestamps: true }
)
);
Мне нужно получить количество всех активных пользователей за определенный месяц (1, 2 и т. Д.)
Я попробовал это.Но этот фрагмент не дает ожидаемого результата
// get all active user count for a specific month
router.post('/report', async (req, res) => {
const selectedMonth = req.body.month;
console.log('month', selectedMonth);
const usersStatus = await UserStatus.aggregate([
{ $project: { month: { $month: '$updatedAt' } } },
{
$match: { $and: [{ month: { $eq: selectedMonth } }, { isActive: true }] }
},
{ $group: { _id: '$user', count: { $sum: 1 } } }
]).exec();
res.status(200).send(usersStatus);
});
Не могли бы вы сказать мне, где я не прав?