MongoDB
 SQL >> Datenbank >  >> NoSQL >> MongoDB

Distinct-Werte über mehrere MongoDB Group-Felder zählen

Zuerst eindeutige Kombinationen mit $group bilden, danach zählen.

db.transactions.aggregate([ { $group: { _id: { employee: "$employee", Month: { $month: "$date" }, Year: { $year: "$date" }, holiday_type: "$holiday_type" } } }, { $group: { _id: { employee: "$_id.employee", Month: "$_id.Month", Year: "$_id.Year" }, count: { $sum: 1 } } } ])

Das entspricht SQL DISTINCT vor COUNT.