2017-05-13 55 views
0

我們有三個集合代表一個類別。當用戶想要查看某個類別中最近發生的事務時,我們需要從這些集合中獲取並顯示。我在stackoverflow中看到一些提及聚合多個集合的問題是不允許的。那麼,有沒有其他的選擇。由於提前聚合多個蒙戈集合和替代

回答

0

如果你是與成千上萬的來自收集的記錄工作,你可以更好的使用MongoDB中「查找」的方法去。對於數十億條記錄來說是不可取的。

鏈接:https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

前,

db.table1.aggregate([ 
// Join with user_info table 
{ 
    $lookup:{ 
     from: "table2",  // other table name 
     localField: "userId", // name of table1 field 
     foreignField: "userId", // name of table2 field 
     as: "t1"   // alias for table1 
    } 
}, 
// Join with user_role table 
{ 
    $lookup:{ 
     from: "table3", 
     localField: "userId", 
     foreignField: "userId", 
     as: "alias name" 
    } 
} 
]);