Martin Ratinaud
Martin Ratinaud

Reputation: 650

How to filter on several populate with mongoose?

For a project I'm using mongoose and am stuck in front of a problem

My Contact has a searcher and a createdBy which I populate (both are users) When I do a search, I want to look for either searcher.name or createdBy.name or an id

if (q) {
    if (q.match(/^[0-9a-fA-F]{24}$/)) filter._id = q;
    else {
      if (!filter.$and) filter.$and = [];
      filter.$and.push({
        $or: [
          { 'searcher.name': { $regex: q, $options: 'i' } },
          { 'createdBy.name': { $regex: q, $options: 'i' } },
        ],
      });
    }
  }

let query = ContactModel.find({ ...filter }).collation({ locale: 'fr', strength: 2 });

Well unfortunately, this does not seem to work. I see that I could use a match in each population but it would not be the same as I would have to filter down not existing populations after

I'm very surprised this behavior is not possible, hence why I come here for help

Upvotes: 0

Views: 21

Answers (0)

Related Questions