【发布时间】:2012-03-09 06:51:42
【问题描述】:
在 MapReduce 方面,我是个菜鸟,我一直在为这个问题而烦恼。希望有人可以帮我一把。
我的目标:获取产品收入和售出数量。
我从中查询的事务集合示例文档:
{ "_id" : ObjectId( "xxxxxxxxxx" ),
"MerchantID" : { "$ref" : "merchants",
"$id" : ObjectId( "xxxxxxxx" ) },
"TransactionSocialKey" : "xxxxxxxx",
"PurchaseComplete: true,
"Products" : [
{ "ProductID" : { "$ref" : "products",
"$id" : ObjectId( "4ecae2b9cf72ab1f6900xxx1" ) },
"ProductPrice" : 14.99,
"ProductQuantity" : "1" },
{ "ProductID" : { "$ref" : "products",
"$id" : ObjectId( "4ecae2b9cf72ab1f690xxx2" ) },
"ProductPrice" : 14.99,
"ProductQuantity" : "1" } ],
"DateTimeCreated" : Date( 1321919161000 ) }
如您所见,我有一个名为 Products 的嵌入式数组,其中包含 ProductID、Product Price 和 Product Quantity。
我的地图功能
map = function(){
if(this.PurchaseComplete === true){
this.Products.forEach(function(Product){
if(Product.ProductID.$id.toString() == Product_ID.toString()){
emit(Product_ID, {
"ProductQuantity" : Product.ProductQuantity,
"ProductPrice" : Product.ProductPrice,
"ProductID" : Product.ProductID.$id.toString()
});
}
});
}
}
因此,我只会发出已完成的事务。如果事务已完成,我将遍历 Products 数组,并且如果 Product.ProductID.$id 等于我在 MapReduce Scope 中设置的 Product_ID,那么我将从集合中发出 Product。
为了测试,我将 Reduce 函数设置为:
reduce = function(key, Product_Transactions){
return {"Transactions" : Product_Transactions};
}
出于某种奇怪的原因,我得到了这种结果:
[results] => Array
(
[0] => Array
(
[_id] => MongoId Object
(
[$id] => 4ecae2b9cf72ab1f6900xxx1
)
[value] => Array
(
[Transactions] => Array
(
[0] => Array
(
[Transactions] => Array
(
[0] => Array
(
[ProductQuantity] => 1
[ProductPrice] => 14.99
[ProductID] => 4ecae2b9cf72ab1f6900xxx1
)
[1] => Array
(
[ProductQuantity] => 1
[ProductPrice] => 14.99
[ProductID] => 4ecae2b9cf72ab1f6900xxx1
)
It Continues…
)
)
[1] => Array
(
[ProductQuantity] => 1
[ProductPrice] => 12.74
[ProductID] => 4ecae2b9cf72ab1f6900xxx1
)
[2] => Array
(
[ProductQuantity] => 1
[ProductPrice] => 12.74
[ProductID] => 4ecae2b9cf72ab1f6900xxx1
)
)
)
)
)
我不确定为什么会得到这个奇怪的嵌入式数组。发射键始终相同且永不改变。我真的很想知道从哪里开始解决问题。任何帮助或指导将不胜感激。
【问题讨论】: