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

265
Vistas
Accessing object in array of arrays using mongoose

I have the following structure and am trying to remove an object in participants (league.division.participants).

var participantSchema = new mongoose.Schema({
player: { type: mongoose.Schema.Types.ObjectId, ref: 'Player' },
record: { type: mongoose.Schema.Types.ObjectId, ref: 'ParticipantRecord' },
events: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Event' } ]
});

var divisionSchema = new mongoose.Schema({
    name: String,
    participants: [ participantSchema ]
});

var leagueSchema = new mongoose.Schema({
    name: String,
    startDate: { type: Date, default: Date.now },
    endDate: Date,
    locked: Boolean,
    leagueType: { type: mongoose.Schema.Types.ObjectId, ref: 'LeagueType' },
    game: { type: mongoose.Schema.Types.ObjectId, ref: 'Game' },
    divisions: [ divisionSchema ],
});

mongoose.model('League', leagueSchema);



var _RemoveDivisionParticipant = function(participantId)
{
    return new Promise((resolve,reject) =>{
        Models.League.findOne({'divisions.participants._id':participantId})
            .populate('divisions')
            .populate('divisions.participants')
            .exec((err, league) => {
                if (err) {return reject(err)}   
                league.divisions(XXXXX).participants(participantId).remove();
                console.log(league.divisions[0].participants[0])
            })
    })
}

This is what i have so far, but obviously it returns the league object, and i have no way of getting to the participants since I don't know which division the participant is in (Shown by XXXXX in the sample). Any pointers as to what I should do?

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

0

You can use $pull to remove an array element based on a condition :

League.update({
    'divisions.participants._id': participantId
}, {
    $pull: {
        'divisions.$.participants': {
            "_id": participantId
        }
    }
}, { multi: true }, function(err, res) {
    console.log(res);
});
over 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