Skip to content

Instantly share code, notes, and snippets.

@itswadesh
Created April 30, 2022 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itswadesh/1d25db6defeabd237747c6447410b019 to your computer and use it in GitHub Desktop.
Save itswadesh/1d25db6defeabd237747c6447410b019 to your computer and use it in GitHub Desktop.
List of accepted friends
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