Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
How to exclude more than one specific documents in mongoose?

Here, I want to exclude the document of the user who is requesting for the response as well as all those users whom the user has already send friendrequests and the users who are already friend with user, how can I achieve this?

From following code I am able to only exclude user data not other's.

try {
      
      const {id} = req.params
      const user = await userModel.findById(id)
   
      const newresponse = await userModel.find({_id:user.friendRequests}).select("_id")
      
      const response = await userModel.find({_id:{"$ne":id}}).sort({creation_date:-1}).limit(5)
      return res.json({succcess:true, response, newresponse})
    } catch (error) {
        console.log(error); 
    }

model:

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
  firstname: String,
  lastname: String,
  password: String,
  email: {
    type: String,
    unique: true,
  },
  birthday: Date,
  creation_date: {
    type: Date,
    default: Date.now,
  },
  gender: String,
  profile_picture: {
    type: String,
    default: "",
  },
  friends:[{
    type:mongoose.ObjectId,
    ref:"userModel"
  }],
  friendRequests:[{
    type:mongoose.ObjectId,
    ref:"userModel"
  }]

});

module.exports = mongoose.model("userModel", userSchema);


almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can concatenate friends and friendRequests arrays and pass that to the filter:

const { id } = req.params;

const user = await userModel.findById(id, 'friends friendRequests');

const excludeUsersArray = user.friends.concatenate(user.friendRequests);
   
const response = await userModel.find({ _id: { "$nin": excludeUsersArray }}).sort({ creation_date:-1 });
      
return res.json({succcess:true, response})
almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda