Mongo mapreduce - 在映射阶段使用 $dateToString
Mongo mapreduce - use $dateToString on mapping stage
我有以下结构的简单元素集合:
{
_id: MongoId
pvs: int
day: IsoDate()
uid: int
}
我想使用 MapReduce
来计算给定用户的综合浏览量,按特定日期范围分组(day/week/month,日期 format
兼容)。
我卡住的是如何在发射前使用 $dateToString
在 map 函数中重新格式化 IsoDate
,所以它发射我想要的格式,比如 %Y-%m-%d
或 %Y-%m
或 %Y-%m-%U
。
当我调用它时,我没有得到重新格式化的日期,但反对 format
和 date
字段。
示例:
function(){
emit(
{'$dateToString': {'format': "%Y-%m-%d", 'date': this.day}},
this.pvs
)}
将return
{
"pvs" : 5
"$dateToString" : {
"format" : "%Y-%m-%d",
"date" : ISODate("2016-07-13T08:27:29.000Z")
}
}
我想改用 returned:
{
"pvs": 5,
"day": "2016-07-13"
}
如果使用 mapReduce,那么您必须创建自己的自定义函数来格式化日期并在您的地图函数中调用它:
dateToString = function(date){
return date.getFullYear() + '-' (date.getMonth() + 1) + '-' + date.getDate();
}
map = function() {
emit(dateToString(this.day), this.pvs);
}
在其 C++ 代码中使用 运行s "within" MongoDB 的聚合框架更好,因此比 V8/spidermonkey 中 运行s 的 mapReduce 更高效(取决于您的版本)捆绑的 JS 控制台中的环境:
db.collectionName.aggregate([
{ "$match": { "uid": userId } },
{
"$project": {
"formattedDate": {
"$dateToString": { "format": "%Y-%m-%d", "date": "$day" }
},
"pvs": 1
}
},
{
"$group": {
"_id": "$formattedDate",
"pvs": { "$sum": "$pvs" }
}
}
])
在学说 mongo odm 中,您可以 运行 您的管道使用 command
函数作为:
$connection = $this->get('doctrine_mongodb')->getConnection();
$mongo = $connection->getMongo();
if (!$mongo) {
$connection->connect();
$mongo = $connection->getMongo();
}
$db = $mongo->selectDB('test_database');
$aggregate_results = $db ->command(array(
"aggregate" => "collectionName",
"pipeline" => array(
array("$match" => array("uid"=> userId )),
array(
"$project" => array(
"formattedDate" => array(
"$dateToString" => array("format" => "%Y-%m-%d", "date"=> "$day")
),
"pvs" => 1
)
),
array(
"$group" => array(
"_id" => "$formattedDate",
"pvs" => array("$sum" => "$pvs")
)
)
)
));
我有以下结构的简单元素集合:
{
_id: MongoId
pvs: int
day: IsoDate()
uid: int
}
我想使用 MapReduce
来计算给定用户的综合浏览量,按特定日期范围分组(day/week/month,日期 format
兼容)。
我卡住的是如何在发射前使用 $dateToString
在 map 函数中重新格式化 IsoDate
,所以它发射我想要的格式,比如 %Y-%m-%d
或 %Y-%m
或 %Y-%m-%U
。
当我调用它时,我没有得到重新格式化的日期,但反对 format
和 date
字段。
示例:
function(){
emit(
{'$dateToString': {'format': "%Y-%m-%d", 'date': this.day}},
this.pvs
)}
将return
{
"pvs" : 5
"$dateToString" : {
"format" : "%Y-%m-%d",
"date" : ISODate("2016-07-13T08:27:29.000Z")
}
}
我想改用 returned:
{
"pvs": 5,
"day": "2016-07-13"
}
如果使用 mapReduce,那么您必须创建自己的自定义函数来格式化日期并在您的地图函数中调用它:
dateToString = function(date){
return date.getFullYear() + '-' (date.getMonth() + 1) + '-' + date.getDate();
}
map = function() {
emit(dateToString(this.day), this.pvs);
}
在其 C++ 代码中使用 运行s "within" MongoDB 的聚合框架更好,因此比 V8/spidermonkey 中 运行s 的 mapReduce 更高效(取决于您的版本)捆绑的 JS 控制台中的环境:
db.collectionName.aggregate([
{ "$match": { "uid": userId } },
{
"$project": {
"formattedDate": {
"$dateToString": { "format": "%Y-%m-%d", "date": "$day" }
},
"pvs": 1
}
},
{
"$group": {
"_id": "$formattedDate",
"pvs": { "$sum": "$pvs" }
}
}
])
在学说 mongo odm 中,您可以 运行 您的管道使用 command
函数作为:
$connection = $this->get('doctrine_mongodb')->getConnection();
$mongo = $connection->getMongo();
if (!$mongo) {
$connection->connect();
$mongo = $connection->getMongo();
}
$db = $mongo->selectDB('test_database');
$aggregate_results = $db ->command(array(
"aggregate" => "collectionName",
"pipeline" => array(
array("$match" => array("uid"=> userId )),
array(
"$project" => array(
"formattedDate" => array(
"$dateToString" => array("format" => "%Y-%m-%d", "date"=> "$day")
),
"pvs" => 1
)
),
array(
"$group" => array(
"_id" => "$formattedDate",
"pvs" => array("$sum" => "$pvs")
)
)
)
));