我将数据存储在这样的模式中
我想知道如何在mongoose中创建一个查询,以获得在过go 7天内创建的用户数,我希望我的输出是这样的
{
对不起,我英语不好
我将数据存储在这样的模式中
我想知道如何在mongoose中创建一个查询,以获得在过go 7天内创建的用户数,我希望我的输出是这样的
{
对不起,我英语不好
您好,我使用mongo aggregate将您每天需要的数据分组如下:
const toDate = new Date("2022-04-08");
const fromDate = new Date("2022-04-01");
const data = await User.aggregate([{
$match: {
createdAt: {
$gte: new Date(fromDate),
$lte: new Date(toDate),
},
},
},
{
$sort: {
createdAt: -1
}
},
{
$project: {
day: {
$dayOfMonth: "$createdAt"
},
month: {
$month: "$createdAt"
},
year: {
$year: "$createdAt"
},
},
},
{
$group: {
_id: {
day: "$day",
year: "$year",
month: "$month",
},
count: {
$sum: 1
},
},
},
{
$project: {
_id: 0,
day: "$_id.day",
month: "$_id.month",
year: "$_id.year",
count: "$count",
},
},
]);