【发布时间】:2017-10-08 22:54:56
【问题描述】:
假设我有以下查询:
post.getSpecificDateRangeJobs = function(queryData, callback) {
var matchCriteria = queryData.matchCriteria;
var currentDate = new Date();
var match = { expireDate: { $gte: new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()) } };
if (queryData.matchCriteria !== "") {
match = {
expireDate: { $gte: new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()) },
$text: { $search: matchCriteria }
};
}
var pipeline = [
{
$match: match
},
{
$group: {
_id: null,
thirtyHourAgo: {
$sum: {
$cond: [
{
$gte: [
"$publishDate",
new Date(queryData.dateGroups.thirtyHourAgo)
]
},
1,
0
]
}
},
fourtyEightHourAgo: {
$sum: {
$cond: [
{
$gte: [
"$publishDate",
new Date(queryData.dateGroups.fourtyHourAgo)
]
},
1,
0
]
}
},
thirtyDaysAgo: {
$sum: {
$cond: [
{
$lte: [
"$publishDate",
new Date(queryData.dateGroups.oneMonthAgo)
]
},
1,
0
]
}
}
}
}
];
var postsCollection = post.getDataSource().connector.collection(
post.modelName
);
postsCollection.aggregate(pipeline, function(err, groupByRecords) {
if (err) {
return callback("err");
}
return callback(null, groupByRecords);
});
};
我想做的是:
1-检查queryData.dateGroups.thirtyHourAgo 是否存在并具有价值,然后仅在查询中添加相关的匹配子句(仅过去 30 小时的帖子计数)。
2-检查queryData.dateGroups.fourtyHourAgo是否存在,然后添加相关查询部分(过去30小时和过去48小时前的帖子计数)。
3 和 queryData.dateGroups.oneMonthAgo 相同(过去 30 小时、48 小时和过去一个月的帖子计数)。
我需要类似 Mysql 的 if 条件来检查变量是否存在且不为空,然后包含查询子句。有可能吗?
我的样本数据是这样的:
/* 1 */
{
"_id" : ObjectId("58d8bcf01caf4ebddb842855"),
"vacancyNumber" : "123213",
"position" : "dsfdasf",
"number" : 3,
"isPublished" : true,
"publishDate" : ISODate("2017-03-11T00:00:00.000Z"),
"expireDate" : ISODate("2017-05-10T00:00:00.000Z"),
"keywords" : [
"dasfdsaf",
"afdas",
"fdasf",
"dafd"
],
"deleted" : false
}
/* 2 */
{
"_id" : ObjectId("58e87ed516b51f33ded59eb3"),
"vacancyNumber" : "213123",
"position" : "Software Developer",
"number" : 4,
"isPublished" : true,
"publishDate" : ISODate("2017-04-14T00:00:00.000Z"),
"expireDate" : ISODate("2017-05-09T00:00:00.000Z"),
"keywords" : [
"adfsadf",
"dasfdsaf"
],
"deleted" : false
}
/* 3 */
{
"_id" : ObjectId("58eb5b01c21fbad780bc74b6"),
"vacancyNumber" : "2432432",
"position" : "Web Designer",
"number" : 4,
"isPublished" : true,
"publishDate" : ISODate("2017-04-09T00:00:00.000Z"),
"expireDate" : ISODate("2017-05-12T00:00:00.000Z"),
"keywords" : [
"adsaf",
"das",
"fdafdas",
"fdas"
],
"deleted" : false
}
/* 4 */
{
"_id" : ObjectId("590f04fbf97a5803636ec66b"),
"vacancyNumber" : "4354",
"position" : "Software Developer",
"number" : 5,
"isPublished" : true,
"publishDate" : ISODate("2017-05-19T00:00:00.000Z"),
"expireDate" : ISODate("2017-05-27T00:00:00.000Z"),
"keywords" : [
"PHP",
"MySql"
],
"deleted" : false
}
假设我的应用程序界面中有三个链接: 1- 30 小时前的帖子。 2- 48 小时前的帖子。 3- 最近一个月的帖子。
现在,如果用户单击第一个链接,我应该控制仅对 30 小时前的帖子进行分组,但如果用户单击第二个链接,我应该准备查询以将帖子分组 30 小时和 48 小时,如果用户点击第三个链接,我应该为所有这些做准备。
我想要类似的东西:
var pipeline = [
{
$match: match
},
{
$group: {
_id: null,
if (myVariable) {
thirtyHourAgo: {
........
........
}
}
if (mysecondVariable) {
fortyEightHourAgo: {
........
........
}
}
【问题讨论】:
-
您能否发布样本数据以进行测试以及该样本的预期输出?通常,与使用聚合框架有关的问题的解决方案是从预期的输出中推断出来的,因此如果您可以提供 IO(输入 -> 输出)数据,它将大大帮助您解决这个问题。
-
@chridam 我已经更新了我的问题,希望知道它足够清楚。
标签: node.js mongodb mongoose aggregation-framework mean-stack