2015-06-13 19 views
0

我有一個包含類似於此文檔的表格:RethinkDB分組和定義輸出

{ 
"title": "title 2", 
"content": "This far, no further!", 
"category": "Fiction" 
} 

這是我使用的查詢:

r.table('posts').group('title').map(lambda item: 
    {'items': item['title']} 
).run()) 

這是輸出:

{ 
    "title 1" : [ 
    { 
     "items" : "title 1" 
    }, 
    { 
     "items" : "title 1" 
    } 
    ], 
    "Title 2" : [ 
    { 
     "items" : "Title 2" 
    }, 
    { 
     "items" : "Title 2" 
    }, 
    { 
     "items" : "Title 2" 
    }, 
    { 
     "items" : "Title 2" 
    } 
    ], 
    "title 3" : [ 
    { 
     "items" : "title 3" 
    } 
    ] 
} 

但是我想得到一個看起來像這樣的輸出結構:

{ 
    "Title 1" : { 
    "item_count" : 3, 
    "items" : [ 
     { 
     "items" : "title 1" 
     }, 
     { 
     "items" : "title 1" 
     }, 
     { 
     "items" : "title 1" 
     } 
    ] 
    }, 
    "Title 2" : { 
    "item_count" : 2, 
    "items" : [ 
     { 
     "items" : "title 2" 
     }, 
     { 
     "items" : "title 2" 
     } 
    ] 
    }, 
    "Title 3" : { 
    "item_count" : 1, 
    "items" : [ 
     { 
     "items" : "title 3" 
     } 
    ] 
    } 
} 

我將如何創建一個查詢來獲得此結果。

回答

0

爲了做到這一點,您需要在您執行ungroup之後執行map

首先,您的第一個查詢的輸出看起來不正確。 RethinkDB不會在map操作後返回一個對象/字典,它會返回一個數組。我從查詢得到的輸出爲以下幾點:

[ 
    { 
    "group": "title 1" , 
    "reduction": [ 
     { 
     "items": "title 1" 
     } , 
     { 
     "items": "title 1" 
     } 
    ] 
    } , 
    { 
    "group": "title 2" , 
    "reduction": [ 
     { 
     "items": "title 2" 
     } , 
     { 
     "items": "title 2" 
     } 
    ] 
    } , 
    ... 
] 

如果你想要的是一個叫items性質與多少個項目的計數items酒店一起,你可以做到以下幾點:

// THIS IS JAVASCRIPT 
r.table('posts') 
.group('title') 
.map(function (item) { 
    return { 
     items: item('title') 
    } 
}) 
.ungroup() 
.map(function (row) { 
    return { 
     'title': row('group'), 
     'items': row('reduction'), 
     'count': row('reduction').count() 
    } 
    }) 

該查詢的結果將是如下:

[ 
    { 
    "count": 2 , 
    "items": [ 
     { 
     "items": "title 1" 
     }, 
     { 
     "items": "title 1" 
     } 
    ], 
    "title": "title 1" 
    }, 
    { 
    "count": 2 , 
    "items": [ 
     { 
     "items": "title 2" 
     } , 
     { 
     "items": "title 2" 
     } 
    ] , 
    "title": "title 2" 
    }, 
    ... 
] 

如果你想要再打開該查詢到對象/字典,這樣就可以通過標題調出帖子,可以使用r.object以及r.args將該帖子數組轉換爲對象。完整的查詢看起來像這樣:

// THIS IS JAVASCRIPT 
r.table('posts') 
.group('title') 
.map(function (item) { 
    return { 
     items: item('title') 
    } 
}) 
.ungroup() 
.map(function (row) { 
    return { 
     'title': row('group'), 
     'items': row('reduction'), 
     'count': row('reduction').count() 
    } 
    }) 
    .do(function (rows) { 
    return r.object(r.args(rows.concatMap(function (row) { 
     return [row('title'), row]; 
    }))); 
    }) 

該查詢的結果將是這樣的:

{ 
    "title 1": { 
    "count": 2 , 
    "items": [ 
     { 
     "items": "title 1" 
     } , 
     { 
     "items": "title 1" 
     } 
    ] , 
    "title": "title 1" 
    } , 
    "title 2": { 
    "count": 2 , 
    "items": [ 
     { 
     "items": "title 2" 
     } , 
     { 
     "items": "title 2" 
     } 
    ] , 
    "title": "title 2" 
    } , 
    ... 
}