Get datetime of last successful login of each user – from logs collection
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// Requires official MongoShell 3.6+ db.getCollection("log").aggregate( [ { $match: { "message": { "$eq": "USER_SUCCESS_LOGIN" } } }, { $sort: { timestamp: -1 } }, { $lookup: { "from": "users", "localField": "meta.user", "foreignField": "_id", "as": "user" } }, { $match: { "user": { $ne: [] } } }, { $unwind: { path: "$user" } }, { $project: { "timestamp": { $dateToString: { format: "%Y-%m-%d %H:%M:%S:%L", date: "$timestamp" }} , "message": 1, "userid": "$user._id", "firstName": "$user.firstName", "lastName": "$user.lastName", } }, { $group: { "_id": "$userid", "firstName": { $first: "$firstName" }, "lastName": { $first: "$lastName" }, "lastLogin": { $first: "$timestamp" } } } ], { "allowDiskUse": true } ); |
