我想在执行 $project 时应用一些简单的字符串操作,是否可以在 $project 上应用类似以下函数的内容? :
var themeIdFromZipUrl = function(zipUrl){
return zipUrl.match(/.*\/(T\d+)\/.*/)[1]
};
我正在使用以下查询:
db.clientRequest.aggregate(
{
$match: {
"l": {$regex: ".*zip"},
"t": { "$gte": new Date('1/SEP/2013'),
"$lte": new Date('7/OCT/2013')
}
}
},
{
$project: {"theme_url" : "$l", "_id": 0, "time": "$t"}
},
{
$group: { _id: {
theme_url: "$theme_url",
day: {
"day": {$dayOfMonth : "$time"},
"month": {$month: "$time"},
"year": {$year: "$time"}
},
},
count: {$sum:1}
}
}
)
这将返回以下内容:
{
"_id" : {
"theme_url" : "content/theme/T70/zip",
"day" : {
"day" : 13,
"month" : 9,
"year" : 2013
}
},
"count" : 2
}
我可以将上面的功能应用到theme_url
字段并将其转换为theme_id
?我稍微了解了一下 Map-Reduce,但我不确定对于这样一个简单的情况来说它是否有点太复杂了。
Thanks,
Amit.