我通过其matchName和teamName找到了该子文档,并且我想显示指定teamName在players []中的内容。日志可帮助您了解我的追求。
这是架构
var MatchSchema({
matchName: {
type: String,
unique: true,
},
teams: [{
teamName: String,
players: [] <------ this is the array I want to display
}]
});
这是findOneAndUpdate
var filter = {matchName: 'matchOne', 'teams.teamName': 'teamOne'}
var update = {$push: {'teams.$.players': 'playerOne'}}
var option = {new: true}
Match.findOneAndUpdate(filter, update, option, function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result); <--- First Test
}
})
第一个测试记录此
{ _id: 5b8899ebe9bc4450811b18a5,
matchName: 'matchOne',
teams:
[ { players: ['playerOne'], _id: 5b8899efe9bc4450811b18a6, teamName: 'teamOne' } ]
} // Logs the Entire Match
第二次考试
Match.findOneAndUpdate(filter, update, option, function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result.teams); <--- Second Test
}
})
第二个测试记录
CoreMongooseArray [
{ players: [ 'playerOne' ], <--- what I want to log
_id: 5b8862912733184d499c2f2e,
teamName: 'teamOne' }
] // Logs the Array of the Teams
现在...我想知道当我指定teamName时如何记录玩家数组。 像...
console.log(teams.teamName = "teamOne".players);
________ 更新 _________
我使用_id方法显示玩家数组
console.log(result.teams.id(req.params.id).players)
我为团队提供了一个_id对象,以便可以将.id方法用于猫鼬
var MatchSchema({
matchName: {
type: String,
unique: true,
},
teams: [{
_id: String, <------ can be used in the .id method to identify this team
teamName: String,
players: []
}]
});