2017-02-01 5 views
1

私はmongodbでグループをやろうとしていますが、私はpymongoインターフェイスを使用しています。私のデータのpymongo aggregate - 各フィールドの返り値

例:

{ 
    "transmitters": [], 
    "receptors": [], 
    "text": "\"We have developed CellExcite, a sophisticated simulation environment for excitable-cell networks. CellExcite allows the user to sketch a tissue of excitable cells, plan the stimuli to be applied during simulation, and customize the diffusion model. CellExcite adopts Hybrid Automata (HA) as the computational model in order to efficiently capture both discrete and continuous excitable-cell behavior.\"", 
    "genes": [], 
    "simenvironment": [ 
     "CellExcite (web link to model)" 
    ], 
    "channels": [], 
    "references": [ 
     112450 
    ], 
    "modelconcepts": [ 
     "Spatio-temporal Activity Patterns", 
     "Simplified Models" 
    ], 
    "celltypes": [ 
     "Heart cell", 
     "Squid axon" 
    ], 
    "title": "CellExcite: an efficient simulation environment for excitable cells (Bartocci et al. 2008)", 
    "modeltype": [ 
     "Neuron or other electrically excitable cell" 
    ], 
    "brainregions": [], 
    "_id": 112468 
}, 

私は細胞の種類ごとのモデルの数を取得したいです。示されているように、モデルはモデルごとに複数の細胞タイプを有することができる。これどうやってするの?ここで

が私の試みである:ここでは

pipeline = [{'$group' : {'_id' : '$celltypes', 'num_models' : {'$sum' : 1}}}, 
      {'$project': {'celltypes':1, 'num_models':1}}] 
for doc in (models.aggregate(pipeline)): 
    pprint (doc) 
    break 

このから私の結果である:

{u'_id': [u'Heart cell'], u'num_models': 6} 
...snip... 
{u'_id': [u'Heart cell', u'Squid axon'], u'num_models': 1} 

私は、出力のために謝罪、私はより多くのモデルを持っている、そしてそれは実際にそれらのすべてを印刷しています。

誰かが私に間違っているかもしれないというヒントを教えてもらえますか?私が欲しいのは、セルタイプのリストとそれらのモデルの数です。

答えて

2

あなたがする必要があるのは、配列であるため$unwindセルタイプです。各値でグループ化できるので別途:

pipeline = [ 
    {'$unwind': '$celltypes'}, 
    {'$group' : { 
     '_id' : '$celltypes', 
     'num_models' : {'$sum' : 1}} 
    }, 
    {'$project': { 
     'celltypes':1, 
     'num_models':1} 
    } 
] 
+0

あなたは命を救っています – Kevin

関連する問題