0

I'm trying to "translate" a query from MySQL,

SELECT a.Nome, a.PosicaoPrimaria, a.PosicaoSecundaria 
FROM Atleta AS a INNER JOIN Equipa AS e ON a.Equipa_idEquipa = e.idEquipa
WHERE e.Localidade = 'Braga' AND a.PosicaoSecundaria != ''
ORDER BY a.PosicaoSecundaria;

to a MongoDB query. So far, so good. I've searched to try and understand how I can manage to do what I want, but I have come to a halt.

So far, this is what I have as a MongoDB query,

db.Atleta.aggregate([ 
 {$lookup: 
  {from:"Equipa", 
   let: {e:"$Equipa_idEquipa", ps:"$PosicaoSecundaria"}, 
    pipeline:[ 
     {$match: 
      {$expr: 
       {$and: [ {$eq:["$idEquipa", "$$e"]}, 
                {$ne:[null, "$$ps"]}, 
                {$eq:["$Localidade", "Braga"]} ] } } }], 
        as:"Query" }},
         {$project:{Query:1, Nome:1, PosicaoPrimaria:1, PosicaoSecundaria:1, _id:0}} ]).pretty()

This gives me the results I want in the players I want to be returned, but it also returns all the other players, who 'don't fit the bill' on the query. See image below.

Query results

What I really need, is to filter the results to only show the players who have the "Query" array as not empty. Is there a way that can be done?

0 Answers0