Below is partnership collection which has users 1 and 2 who participated. One of the users is the current logged in user available as req.user._id
and I am listing the user's partners.
{
"_id": {
"$oid": "6381697ddddbdb42b4682fb7"
},
"users": [{
"$oid": "user_one_id"
}, {
"$oid": "user_two_id"
}],
}
I am getting the fields of the user who is partner from PartnerModel (excluding the req.user._id). Assuming in this case, req.user._id
is user_one_id
let partners =
await userPartnerModel.find({
users: { $in: [req.user._id] }
}).sort({ createdAt: -1 })
.populate({
path: 'users',
select: 'profilePic firstName',
model: userModel
})
res.status(200).json(partners)
The issue is that partners result also includes the req.user.
The result I am getting now is:
{
"users":[
{
"firstName":"User One",
"profilePic":"https://image.jpg",
"_id":"63729a9a73f736476cbdd0cc"
},
{
"firstName":"User Two",
"profilePic":"https://image2.jpg",
"_id":"63728ae473f736476cbdd0be"
}
But the results should not include req.user._id
which is "User One"
Hey you can use the select
function to allow only the field you want. By default the _id field is allowed you can disallow it by adding -id
. For your case you can use this query:
let partners =
await userPartnerModel.find({
users: { $in: [req.user._id] }
}).sort({ createdAt: -1 })
.populate({
path: 'users',
select: 'profilePic firstName -_id',
model: userModel
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.