I have two collections. The “Users” collection has all the users and “Logs” collection has all the users’ daily logs. I need to find the users who dont have any logs between two dates.
I am able get the users who have data in logs collection between 2 dates but not able to get the users who dont have any data in logs collection between given dates.
Most likely you did not. The $match inside the $lookup pipeline will find logs of the users between the from and to date. So a user with logs will have a non empty array and a user without logs will have an empty array. Then
which means, that only users with an empty array will be matched. That is users with no logs.
Now that we have something working a little optimization is in order.
Since you only want the users that have 0 logs, there is no point to $lookup all the logs of a given user. We can stop when we find the first one. To do that we add $limit:1 to the $lookup pipeline: right after the $match. Like: