猫鼬的日期比较没有时间和分组多个属性? [英] mongoose Date comparing without time and Group by multiple properties?
问题描述
可能的问题是重复,但对此表示歉意.因为我们的业务案例不同于现有的问题.
Might be question is look like duplicate but apologize for it. Because our business case is different than existing question.
我们使用 Nodejs 和 MongoDB 编写REST API.
We are using the Nodejs and MongoDB for writing the REST API.
我有一个名为: EMPLog 的集合,其中包含以下文档对象.
I am having collection called : EMPLog with following document object.
{
"_id" : ObjectId("5f351f3d9d90b1281c44c5dp"),
"staffId" : 12345,
"category" : "trend",
"page_route" : "http://example.com/rer",
"expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
"createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
"updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
"__v" : 0
}
{
"_id" : ObjectId("5f351f3d9d90b1281c44c5de"),
"staffId" : 12346,
"category" : "incident",
"page_route" : "http://example.com/rergfhfhf",
"expireAt" : ISODate("2020-08-12T11:08:45.196Z"),
"createdAt" : ISODate("2020-08-12T11:08:45.199Z"),
"updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
"__v" : 0
}
{
"_id" : ObjectId("5f351f3d9d90b1281c44c5dc"),
"staffId" : 12347,
"category" : "trend",
"page_route" : "http://example.com/rerrwe",
"expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
"createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
"updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
"__v" : 0
}
{
"_id" : ObjectId("5f351f3d9d90b1281c44c5dr"),
"staffId" : 12348,
"category" : "trend",
"page_route" : "http://example.com/rerrwe",
"expireAt" : ISODate("2020-08-12T11:08:45.196Z"),
"createdAt" : ISODate("2020-08-12T11:08:45.199Z"),
"updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
"__v" : 0
}
我们正在接收来自用户的类别和 createdAt 的输入. createdAt 收到的邮件没有时间.
we are receiving the input from as category and createdAt from user. createdAt is receiving without time.
假设,用户提供的类别为趋势,并以 2020-08-13 创建,则我们必须按趋势分组, createdAt 和 staffId ,然后将 staffId 作为数组返回.
Suppose , User is providing the category as trend and createdAt as 2020-08-13 then We have to group by trend,createdAt and staffId and return the staffId as array.
注意:类别和 CreatedAt 将接收来自用户的动态/运行时间.
note: Category and CreatedAt will receiving dynamic/runtime from user.
预期结果是:{data:{staffIds:[12345,12347]}}
如果有人可以指导我,那将是很大的帮助.
if anyone can guide me then it will be great help.
首先感谢所有专家.
推荐答案
您可以尝试使用$match
匹配给定的category
和createdAt
日期,然后进行分组,最后使用$addToSet
来获取所有唯一的staffId
s:
You can try to match the given category
and createdAt
-date using $match
, then group and finally use $addToSet
to get all the unique staffId
s:
db.collection.aggregate([
{
$match: {
"category": "trend",
"createdAt": {$gte: new Date("2020-08-13T00:00:00Z"), $lt: ISODate("2020-08-14T00:00:00Z")}
}
},
{
"$group": {
"_id": null,
"staffIds": {
"$addToSet": "$staffId"
}
}
}
]);
以下是mongoplayground上的示例(由于不支持ISODate,因此必须在日期中使用字符串和正则表达式): https://mongoplayground.net/p/oGM7cfvCRQx
Here's an example on mongoplayground (had to use strings and regex for the date as ISODate is not supported there): https://mongoplayground.net/p/oGM7cfvCRQx
这篇关于猫鼬的日期比较没有时间和分组多个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!