Filter by values in field with hasOne Relationship

Expected behavior

Have a collection with a hasOne relationship e.g. field: { type: mongoose.Schema.Types.ObjectId, ref: ‘accounts’ }
When trying to filter that this field is not blank, must select a sub field, I assume for there to be an aggregation. But my collection is too big and that aggregation times out. Want to just filter on values of the field. For examplein this particular case I want to make a filter “is present” on the field itself, not subfields

Actual behavior

What is the current behavior?

Failure Logs

Please include any relevant log snippets, if necessary.


Please provide any relevant information about your setup.

  • Package Version:
  • Express Version:
  • Sequelize Version:
  • Database Dialect:
  • Database Version:
  • Project Name:

Hi @Liam_Cheung,

Thank you for sharing this issue.

If I understand well, you want to check if the relation exists, without checking a particular attribute of said related item?
eg: you want to filter users having a team, but without any consideration on that team.

Is that right?

Yes that is correct. And also create segments that way.

I am very sorry @Liam_Cheung but this is not possible at the moment.

Let me share your feedback with the team and have it put on our roadmap.

You might be able to achieve the wanted result by using a SmartField.
Please have a look at our documentation here.

I think a smart field would not be an efficient query and with the size of my collections would not work. For now I created a smart segment, but of course that is not ideal, since I will have to release new code when users want another variation fo the filter. Keep me informed if it makes it to the top of the roadmap.

Hey @Liam_Cheung,

If you just want to check that a relationship is present or not, why don’t you just filter on the subfield id
Something like: is present or the opposite is blank.

I hope this can help you

I assume there is some aggregation going on here with the related collection because on a big dataset, the aggregation is causing the query to timeout, whereas if you just query on the main collection with exists: false, it is not problem. I ended up having to create a custom segment around this.