Created
April 30, 2022 09:28
-
-
Save itswadesh/1d25db6defeabd237747c6447410b019 to your computer and use it in GitHub Desktop.
List of accepted friends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const query = [ | |
{ | |
$lookup: { | |
from: 'friends', | |
localField: '_id', | |
foreignField: 'userId', | |
as: 'friends', | |
}, | |
}, | |
{ | |
$project: { | |
_id: 1, | |
name: 1, | |
email: 1, | |
isFriend: { | |
$cond: { | |
if: { | |
$in: [ObjectId(userId), '$friends.friendId'], | |
}, | |
then: true, | |
else: false, | |
}, | |
}, | |
}, | |
}, | |
]; | |
try { | |
const users = await User.aggregate(query); | |
return users; | |
} catch (error) { | |
throw error; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment