Home > database >  How to use mongoose to filtered the deleted users?
How to use mongoose to filtered the deleted users?

Time:06-17

Heyyy guys ,

I need a function that ,when you delete a user ,you and that user been deleled won't see each orther in their users list . I am not sure how can I make this happen .Could you please help me ?

Thank you so much in advance !

User schema :

const mongoose = require("mongoose");


const userSchema = new mongoose.Schema(
  {
   
    userName: {
      type: String,
    },

    deleteList: [{ type: mongoose.Types.ObjectId, ref: "User" }],
    beenDeletedList: [{ type: mongoose.Types.ObjectId, ref: "User" }],
  }

);



const User = mongoose.model("User", userSchema);

module.exports = UserInfo;

Get all filted users (I am not sure how can filter all the users been deleted and show the rest of the users) :

exports.getSortedUsers = async (req, res, next) => {
   const user = await User.findById(req.body._id);

}

CodePudding user response:

You can exclude an array of object from mongoose find with the $nor operator.

In your case, you can try something like this :

exports.getSortedUsers = async (req, res, next) => {
   const users = await User.find({ $nor: beenDeletedList });
}

With beenDeletedList is the list of users blocked (deleted) by you.

  • Related