Home > Net >  Is there a way of filtering mongodb data?
Is there a way of filtering mongodb data?

Time:10-13

const users = await User.find();

if (!users)
      return res.status(404).json({ success: false, msg: "No user found" });

users.filter((u) => u._id !== req.user._id);

I don't want to select req.user._id when I get all of the users. When I use filter method, it doesn't work. It still includes req.user._id in the data. Is there a way to handle this in a short way ?

CodePudding user response:

You can use $ne

await User.find({
  _id: {
    $ne: ObjectId(req.user._id.toString())
  }
})

or you can use $nin if you have multiple ids

await User.find({
  _id: {
    $nin: [ObjectId(req.user._id.toString())]
  }
})
  • Related