如何使用mongoose过滤已删除的用户?



大家好,

我需要一个函数,当你删除一个用户时,你和那个被删除的用户不会在他们的用户列表中看到对方。我不知道怎样才能做到这一点,你能帮助我吗?

提前谢谢你!

用户模式:

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;

获取所有筛选过的用户(我不确定如何过滤所有已删除的用户并显示其余用户):

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

您可以使用$nor操作符从mongoose find中排除一个对象数组。

在您的情况下,您可以尝试这样做:

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

使用beenDeletedList是被您阻止(删除)的用户列表。

最新更新