If you just need to get usersIds from example collection and then just count how many users with that ids are online, bbetter to collect those ids into one array and then $lookup with that array of ids.
db.getCollection('example').aggregate([
{
$group: {
_id: null,
usersIds: {
$addToSet: '$userId',
},
},
},
{
$lookup: {
from: 'users',
let: {
usersIds: '$usersIds',
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $in: ['$_id', '$$usersIds'] },
{ $eq: ['$isLogin', true] },
],
},
},
},
],
as: 'users',
},
},
{
$project: {
_id: null,
totalUsers: {
$size: '$users',
},
},
},
]);