gsd
gsd

Reputation: 33

Can a MongoDB compound index have uniqueness on a subset of the fields?

I'd like to use a MongoDB unique compound index (with two fields) as a covering index by adding two more fields. Can I specify the uniqueness of the four field index is defined by the first two fields only?

Reading the documentation it sounds like I may have to have one compound four field index for the covering, and another two field index purely for asserting the uniqueness constraint.

Upvotes: 0

Views: 68

Answers (1)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

You are right that you need to indices for achieving what you want. And there is nothing wrong with it. While uniqueness is checked during writes (and the according index will be used for it), the other index will either be used automatically or you can hint MongoDB to use it.

Upvotes: 0

Related Questions