0

I have 2 collections - competition and product. Competition contains the product objectID and product contains competition_status.

What I'm trying to do is ONLY display the competitions whose product has a competition_status of "Complete" or "Current".

I've implemented this, however, I am also seeing the competitions that had a "Pending" status, just the product objects is null for these two. I somehow need these competitions removed from the competitions object that is outputted.

api.mongo.competition.find(filter)
      .populate('product', 'name competition_status', { competition_status: { $in: ['Complete', 'Current'] } })


      .exec(
        function(err, competitions) {
          if(err) {
            next(err, {});
            return;
          }
          next(err, competitions);
        });
DT.DTDG
  • 765
  • 1
  • 13
  • 31

1 Answers1

0

Try using

{ competition_status: { $nin: ['Pending'] }

instead of

{ competition_status: { $in: ['Complete', 'Current'] }

you can get more info here $nin

from populate as join populate is not a join, it's just a convenience function to follow up the main query with additional queries to pull in the associated data from other collections. You need to have

{ competition_status: { $in: ['Complete', 'Current'] } 

in

mongo.competition.find()

Community
  • 1
  • 1
Dinkar Thakur
  • 3,025
  • 5
  • 23
  • 35
  • Thanks for the answer. Won't this just adjust the "in"/"not in"? Seems as though the current way I have it where I use `populate` doesn't work? – DT.DTDG May 20 '15 at 06:54
  • Have you mapped the competition and product. this link might help http://mongoosejs.com/docs/populate.html i guess you are missing the mapping. – Dinkar Thakur May 20 '15 at 07:36