Should I store ref or ref.id as foreign key on a collection?

So if I have two collections, example:

collections: users, tokens
relationship: one user may have many tokens (one-to-many)

should I store ref.id as userId on tokens collection or should I store the ref itself as userRef on tokens collection?

Hi!

I tend to use the ref itself due to the following reasons:

  1. You can take advantage of the CurrentIdentity() function when searching terms on Indexes that retrieves information related to the current user session.
  2. You can access the id field in the ref using a Select(‘id’, YOUR_REF) statement
  3. If you have Indexes without defined values (fields that should be reported in search results), storing the ref could simplify Intersections or Unions operations, since those Indexes returns a ref by default.

You can surely store the id (or both), it would depend on your app needs.

All the best.

1 Like