2014-04-03 28 views
0

它就像我需要:我無法獲取數據來聚合它。 PHP + MongoDB的

$out = $collection->aggregate(
     array(
      '$match' => array('type' => 'chair') 
     ), 
     array(
       '$project' => array(
        'chairtype' => 1, 
        'mijczjeqeo'=>1 
       ) 
     ), 
     array( 
      '$group' => array(
       '_id' => '$chairtype', 
       'MIDDLE_mijczjeqeo' => array('$avg' => '$mijczjeqeo'), 
       'SUMMA__mijczjeqeo' => array('$sum' => '$mijczjeqeo') 
      ) 
     ) 
); 
my_dump($out); 

,但我需要從陣列得到同樣的文檔的聚集真正的數據:版本[0] [內容] [mijczjeqeo]

請更正我的腳本。它不工作:

$out = $collection->aggregate(
     array(
      '$match' => array('type' => 'chair') 
     ), 
     array(
       '$project' => array(
        'chairtype' => 1, 
        'versions.0.content.mijczjeqeo'=>1 
       ) 
     ), 
     array( 
      '$group' => array(
       '_id' => '$chairtype', 
       'MIDDLEmijczjeqeo' => array('$avg' => '$versions.0.content.mijczjeqeo'), 
       'SUMMAmijczjeqeo' => array('$sum' => '$versions[0]["content"]["mijczjeqeo"]') 
      ) 
     ) 
); 

沒有一個方法不起作用:

'MIDDLEmijczjeqeo'=>數組( '$平均'=> '$ versions.0.content.mijczjeqeo')

'SUMMAmijczjeqeo'=> array('$ sum'=>'$ versions [0] [「content」] [「mijczjeqeo」]')

我覺得問題近.0。

我嘗試做在蒙戈控制檯...

db.documents.aggregate({$match:{'type':'chair'}},{$project:{'chairtype': 1, 'mijczjeqeo':1}},{$group:{'_id':'$chairtype','MID':{$avg:'$mijczjeqeo'}}}) 
{ 
    "result" : [ 
     { 
      "_id" : "T", 
      "MID" : 6.615384615384615 
     }, 
     { 
      "_id" : "G", 
      "MID" : 8.310344827586206 
     }, 
     { 
      "_id" : "E", 
      "MID" : 6.9523809523809526 
     } 
    ], 
    "ok" : 1 
} 

db.documents.aggregate({$match:{'type':'chair'}},{$project:{'chairtype': 1, 'versions.0.content.mijczjeqeo':1}},{$group:{'_id':'$chairtype','MID':{$avg:'$versions.0.content.mijczjeqeo'}}}) 
{ 
    "result" : [ 
     { 
      "_id" : "T", 
      "MID" : 0 
     }, 
     { 
      "_id" : "G", 
      "MID" : 0 
     }, 
     { 
      "_id" : "E", 
      "MID" : 0 
     } 
    ], 
    "ok" : 1 
} 
+0

''versions [0] [「content」] [「mijczjeqeo」]''應該是''versions.0.content.mijczjeqe'' – Sammaye

+0

像第一個一樣,它不起作用((( – user3469031

+0

Sammaye,非常感謝您的幫助!! – user3469031

回答

0

那麼你不能像投影在聚合管道。如果您想要對聚合語句中的數組元素採取行動,則首先需要$unwind該數組,然後使用$match所需的元素,或者在您的情況下使用另外的$group階段選擇$first項目。

你的問題不顯示文檔的結構,所以我就用一個樣品,我的「椅子」收藏:

{ 
    "_id": 1, 
    "type": "chair", 
    "chairtype": "A", 
    "versions": [ 
     { 
      "revision": 1, 
      "content": { 
       "name": "ABC", 
       "value": 10 
      } 
     }, 
     { 
      "revision": 2, 
      "content": { 
       "name": "BBB", 
       "value": 15 
      } 
     } 
    ] 
} 
{ 
    "_id": 2, 
    "type": "chair", 
    "chairtype": "A", 
    "versions": [ 
     { 
      "revision": 1, 
      "content": { 
       "name": "CCC", 
       "value": 20 
      } 
     }, 
     { 
      "revision": 2, 
      "content": { 
       "name": "BAB", 
       "value": 12 
      } 
     } 
    ] 
} 

很少,但足以讓點。現在聚集聲明:

db.chairs.aggregate([ 

    // Normal query matching, which is good 
    { "$match": { "type": "chair" } }, 

    // Unwind the array to de-normalize 
    { "$unwind": "$versions" }, 

    // Group by the document in order to get the "first" array element 
    { "$group": { 
     "_id": "$_id", 
     "chairtype": { "$first": "$chairtype" }, 
     "versions": { "$first": "$versions" } 
    }}, 

    // Then group by "chairtype" to get your average values 
    { "$group": { 
     "_id": "$chairtype", 
     "MID": {"$avg": "$versions.content.value"} 
    }} 

]) 

當然,如果你的實際文檔嵌套數組,那麼你將是「平倉」和「匹配」所需要的元素。但這是將數組內容「縮小」到所需元素的一般過程。

+0

謝謝。它運作良好。 – user3469031

+0

請參閱:http://stackoverflow.com/questions/23063840/aggregate-mongo-data-php謝謝 – user3469031