0

I have a Schema

_id:"5ff9b57d2c196236206a0e96"
eventId:"1"
eventName:"Name"
teams:[
 0: {player:"5245552555556"}
 1: {player:"5245552559557"}
 2: null
]

Expected result should be like

_id:"5ff9b57d2c196236206a0e96"
eventName:"Name"
teams:[
 0: {player: {
      firstName : "player1"
      lastName : "player1"
      email : "player1@email.com"
     }
 1: {player: {
      firstName : "player2"
      lastName : "player2"
      email : "player2@email.com"
     }
    }
]

I am running the below query to get the result but if it finds any null value it doesn't return that whole document instead what I need is just to ignore that particular field with null value and populate the rest of the fields

const teams = await Event.find({_id:eventId },{"teams":{$ne:null"} }).populate(
        "teams.player",
        "firstName lastName email "
      );
Ryuk
  • 362
  • 1
  • 6
  • 21
  • Have you tried to populate directly first? I think population automatically ignores fields with a null value. – Algo7 Jan 11 '21 at 18:02
  • Yes I have tried but it gives the error... Cann't populate the value of null – Ryuk Jan 11 '21 at 19:19
  • Then you might have to go with aggregation. Check out this post: https://stackoverflow.com/questions/52150132/how-to-exclude-null-values-from-mongoose-populate-query . It is the same problem, but yours might not require that many creteria as in the post above. – Algo7 Jan 11 '21 at 19:43

0 Answers0