'How to take users from a table that are not in another collection?
I want to take users who are not in another collection (UserRates)
My code:
const rateUsers = await UserRates.find({ }).populate('user');
const users = await User.find({ status: "active" }, { user: { $nin: { rateUsers } } })
is don`t work
Most likely I'm doing it all wrong. Can you suggest how this can be done? There is a user (ObjectID) field in the UserRates collection and I need to take those who are not in UserRares in the User collection
Solution 1:[1]
and i try it:
const users = await User.aggregate([
{
"$lookup": {
"from": "user_rates",
"localField": "_id",
"foreignField": "user",
"as": "rates"
}
},
{
"$match": {
"rates.user": {
"$exists": false
}
}
}
])
it work!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ????? ????????? |
