I am in desperate need of a solution for this mongoose-related issue. My tech stack includes Express, Mongoose, and GraphQL.
To give you some context, I have two collections: users and groups.
user {
name: string,
groupAsMember: [groups],
status: boolean
}
group {
name: string,
members: [user],
status: boolean
}
The problem I'm facing is removing a user document from the group's user array when the user's status is set to false.
Here's what I've tried so far:
return User.findOneAndUpdate(
{_id: id},
{$set: {status: false}},
{new: true, useFindAndModify: false}
)
.exec()
.then(async (user) => {
return await Group.updateMany(
{'_id': {$in: user.groupAsMember}},
{$pull: {"members.id": user._id}},
{new: true, useFindAndModify: false}
)
.then((user) => {
console.log(user);
return user;
})
.catch((err) => {
console.log(err);
return err;
})
})
.catch((err) => console.error(err));
I have experimented with different online solutions as shown in the commented-out code snippets. However, I continue to encounter errors like:
Cannot use the part (id) of (members.id) to traverse the element ({members: [ ObjectId('5f985ffc59355439ac59598e'), ObjectId('5f9a328c83eb44243021d756') ]})
unknown top level operator: $eq
The positional operator did not find the match needed from the query.
If you have the answer to this puzzling issue, your help would be greatly appreciated. Thank you in advance.