In my DB, I have collection “totalpoints” with the following sample document:
{
"_id": {
"playerId": "113069",
"tournamentId": "197831"
},
"playerName": "xyz",
"pointsTotal": 0,
"__v": 0
}
For one of my other collection named “teams”, I defined the following schema:
const teamsSchema = new Schema({
_id: {
teamName: String,
tournamentId: String
},
tournamentName: String,
player1: {
type: Schema.Types.ObjectId,
ref: 'PointTotal',
}
})
What I get in team collection in monogodb is this document:
{
"_id": {
"teamName": "BILALATI6449",
"tournamentId": "197831"
},
"tournamentName": "Amanora Cricket Championship 2021",
"player1": {
"playerId": "113069",
"tournamentId": "197831"
},
"_v": 0
}
But, when I execute the following code:
const doc = await Team.findOne({
"_id.teamName": "BILALATI6449",
"_id.tournamentId": "197831"
}).populate("player1")
console.log(doc)
the following result prints to console and .populate doesn’t do its job:-
{
_id: { teamName: 'BILALATI6449', tournamentId: '197831' },
tournamentName: 'Amanora Cricket Championship 2021',
__v: 0
}
Can someone help me to figure out what the problem is?
Here’s the full document sample from the team collection. I have simplified it above:
{
"_id": {
"teamName": "BILALATI6449",
"tournamentId": "197831"
},
"tournamentName": "Amanora Cricket Championship 2021",
"player1": {
"playerId": "2331078",
"tournamentId": "197831"
},
"player2": {
"playerId": "196713",
"tournamentId": "197831"
},
"player3": {
"playerId": "113069",
"tournamentId": "197831"
},
"player4": {
"playerId": "249044",
"tournamentId": "197831"
},
"player5": {
"playerId": "113129",
"tournamentId": "197831"
},
"player6": {
"playerId": "181056",
"tournamentId": "197831"
},
"player7": {
"playerId": "658022",
"tournamentId": "197831"
},
"player8": {
"playerId": "182623",
"tournamentId": "197831"
},
"player9": {
"playerId": "249047",
"tournamentId": "197831"
},
"player10": {
"playerId": "658053",
"tournamentId": "197831"
},
"player11": {
"playerId": "658057",
"tournamentId": "197831"
},
"__v": 0
}