5

MongoDB 3.2 introduced partial indexes - you can define a filter as part of the index to which the index will be applied. In ruby's mongoid I can define a unique or normal index.

Since the partial index contains a filter expression on which the index should work - how do you define it on a model ?

Doron
  • 3,176
  • 7
  • 35
  • 60
  • You would just add a scope with the same filter on your model. mongo's query planner should pick it up when it executes – Anthony Jun 27 '18 at 13:02
  • 2
    Had the same question. See this answer https://stackoverflow.com/a/55590312/1257369 – everyman Apr 09 '19 at 10:11

1 Answers1

1

To replicate @everyman's answer to this question (please upvote his answer too).


For Mongoid users:

index(
  { user_id: 1, author_id: 1, complete: 1 },
  background: true,
  partial_filter_expression: {
    complete: { :$eq => true }
  }
)

Couldn't find any docs, but this PR.

Stefan Collier
  • 4,314
  • 2
  • 23
  • 33