Muhammad Danyal
Muhammad Danyal

Reputation: 61

Why my Mongoose populate request does not work?

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 collections 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 MongoDB 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
}

Here's the full document sample form team collection:

{
  "_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
}

Upvotes: 0

Views: 38

Answers (1)

mohammad Naimi
mohammad Naimi

Reputation: 2359

use aggregate on team collection

db.team.aggregate([
  {
    "$match": {
      "_id.teamName": "BILALATI6449",
      "_id.tournamentId": "197831"
    }
  },
  {
    "$lookup": {
      "from": "totalpoints",
      "localField": "player1.playerId",
      "foreignField": "_id.playerId",
      "as": "player1"
    }
  }
])

https://mongoplayground.net/p/nGJSZ6kJMIc

and if for you new sample data with multiple player number use this

db.team.aggregate([
  {
    "$match": {
      "_id.teamName": "BILALATI6449",
      "_id.tournamentId": "197831"
    }
  },
  {
    "$addFields": {
      "newField": {
        "$filter": {
          "input": {
            "$objectToArray": "$$ROOT"
          },
          "as": "p",
          "cond": {
            "$regexMatch": {
              "input": "$$p.k",
              "regex": ".*player*."
            }
          }
        }
      }
    }
  },
  {
    "$lookup": {
      "from": "totalpoints",
      "localField": "newField.v.playerId",
      "foreignField": "_id.playerId",
      "as": "players"
    }
  },
  {
    "$project": {
      _id: 1,
      tournamentName: 1,
      players: 1
    }
  }
])

https://mongoplayground.net/p/ctGybQzGrW7

Upvotes: 1

Related Questions