使用mongoose访问数组数组中的对象

我有以下结构,并试图删除参与者(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]) }) }) } 

这是我到目前为止,但显然它返回的联赛对象,我无法得到的参与者,因为我不知道参与者在哪个部门(示例中的XXXXX)。 任何指针,我应该做什么?

你可以使用$pull来删除一个基于条件的数组元素:

 League.update({ 'divisions.participants._id': participantId }, { $pull: { 'divisions.$.participants': { "_id": participantId } } }, { multi: true }, function(err, res) { console.log(res); });