Counts log entries, with type equal 'CREATE’ , fetch patients data from another collection, remove empty documents (if patients was not found the field is empty array), count by provided user id.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var user_id = '5ad070640c9faddd41a4b665' db.getCollection('log').aggregate([ { $match: { message: {$eq: 'CREATE' } }}, { $lookup: { from: 'patients', localField: 'meta.patient', foreignField: '_id', as: 'patient' } }, { $match: { patient: {$not: {$size: 0}} }}, { $match: {'meta.user': {$eq: ObjectId(user_id)}}}, { $count: "created" } ]) |
