2015-10-15 156 views
0

因此,這裏是一個文檔的樣品中的mongodb我的收藏:MongoDB的PHP查詢與嵌套對象

{ 
    "_id" : ObjectId("561e0de61c9218b7bf9877c3"), 
    "Date" : NumberLong(20151014), 
    "Hour" : NumberLong(10), 
    "ProductId" : ObjectId("5614ba9c2e131caa098b4567"), 
    "ProductName" : "Test", 
    "ProducerId" : ObjectId("5617802151f8adf4db329d52"), 
    "ProducerName" : "Producer", 
    "ProducerRate" : NumberLong(300), 
    "ProducerMedium" : "Emailer", 
    "TotalLead" : NumberLong(6), 
    "VerifiedLead" : NumberLong(3), 
    "UnverifiedLead" : NumberLong(2), 
    "UnQualifiedLead" : NumberLong(1), 
    "TotalEarning" : NumberLong(660), 
    "Consumers" : [ 
     { 
      "ConsumerId" : ObjectId("5617802151f8adf4db329d54"), 
      "ConsumerName" : "Consumer1", 
      "ConsumedRate" : NumberLong(120), 
      "ConsumedLead" : NumberLong(3), 
      "Earning" : NumberLong(360) 
     }, 
     { 
      "ConsumerId" : ObjectId("5617802151f8adf4db329d58"), 
      "ConsumerName" : "Consumer2", 
      "ConsumedRate" : NumberLong(100), 
      "ConsumedLead" : NumberLong(3), 
      "Earning" : NumberLong(300) 
     } 
    ] 
} 

現在我想從PHP中的數據庫中ConsumerId和產品編號分組的ConsumedLead。

我已經做了這麼遠給我TotalLead和VerifiedLead產品編號分組,但不知道如何獲得consumerbased結果相同:

$keyf = new MongoCode('function(doc) {  
       return {\'ProductId\': doc.ProductId,\'ProductName\': doc.ProductName}; 
     }'); 
     $initial = array('TotalLead'=>0,'VerifiedLead'=>0); 

     $reduce = "function(obj, prev) { 
     prev.TotalLead += obj.TotalLead; 
     prev.VerifiedLead += obj.VerifiedLead; 

     }"; 
     $result = $collection->group($keyf, $initial, $reduce); 
     var_dump($result); 

任何幫助請。

編輯: 預期的結果wpuld是:

{ [0]=> array(4) { ["ProductId"]=> object(MongoId)#8 (1) { ["$id"]=> string(24) "5614ba9c2e131caa098b4567" } ["ProductName"]=> string(4) "Test" ["ConsumerId"]=> object(MongoId)#8 (1) { ["$id"]=> string(24) "5617802151f8adf4db329d58" } ["ConsumedLead"]=> float(4) } } 

回答

1

的解決方案是使用aggregation framework其中操作包括$unwind操作者初始流水線級,因爲這會從解構Consumers陣列字段輸入文檔並輸出每個元素的文檔。每個輸出文檔用一個元素值替換數組。於是,這將有可能使在$group步工作$sum組蓄電池操作,因而givies你所需ConsumedLead通過ConsumerIdProductId分組:

db.collection.aggregate([ 
    { 
     "$unwind": "$Consumers" 
    }, 
    { 
     "$group": { 
      "_id": { 
       "ProductId": "$ProductId", 
       "ConsumerId": "$Consumers.ConsumerId" 
      }, 
      "TotalConsumedLead": { 
       "$sum": "$Consumers.ConsumedLead" 
      } 
     } 
    } 
]) 

在上面的示例運行此聚合操作將導致:

/* 0 */ 
{ 
    "result" : [ 
     { 
      "_id" : { 
       "ProductId" : ObjectId("5614ba9c2e131caa098b4567"), 
       "ConsumerId" : ObjectId("5617802151f8adf4db329d58") 
      }, 
      "TotalConsumedLead" : NumberLong(3) 
     }, 
     { 
      "_id" : { 
       "ProductId" : ObjectId("5614ba9c2e131caa098b4567"), 
       "ConsumerId" : ObjectId("5617802151f8adf4db329d54") 
      }, 
      "TotalConsumedLead" : NumberLong(3) 
     } 
    ], 
    "ok" : 1 
} 

因此,最終的工作aggregation in PHP應該是:

$pipeline = array( 
    array('$unwind' => '$Consumers'), 
    array(
     '$group' => array(
      '_id' => array(
       'ProductId' => '$ProductId', 
       'ConsumerId' => '$Consumers.ConsumerId', 
      ), 
      'TotalConsumedLead' => array(
       '$sum' => '$Consumers.ConsumedLead' 
      ), 
     ) 
    ), 
); 

$out = $collection->aggregate($pipeline ,$options); 
+1

謝謝,完美的工作:) – Nero

+0

@Nero真棒,很高興幫助:-) – chridam