我不認爲流星的Minimongo驅動程序包含聚合框架的幫助,但至少有一個問題有suggestions on how to call the underlying MongoDB aggregate
command。
假設你的數據是這樣的:
db.things.aggregate(
{ $group: {
_id: { type: "$type", color: "$color" }
}}
)
結果會是什麼樣子:
{
"result" : [
{
"_id" : {
"type" : "food",
"color" : "blue"
}
},
{
"_id" : {
"type" : "animal",
"color" : "blue"
}
},
{
"_id" : {
"type" : "animal",
"color" : "red"
}
}
],
"ok" : 1
}
db.things.insert([
{ type: 'animal', color: 'red', owner: 'paul'},
{ type: 'animal', color: 'red', owner: 'jack'},
{ type: 'animal', color: 'blue', owner: 'paul'},
{ type: 'food', color: 'blue', owner: 'jack'}
])
可以使用MongoDB的Aggregation Framework和$group
operator做在服務器端這個分組
一個更好的函數可能是'function(item){return JSON.stringify ({type:item.type,color:item.color}); }' – sbking
同意,Cuberto! – richsilv