how to create a unique index that allows duplicates for null and other specified values?

Ok, did not get an answer on this site, but I have eventually figured it out. The index had to be defined in the following way to ensure it’s only accepting unique values for key “header.header.message-id”, except for null values and those that equal to “#1/1”.

  {
    "key": {
      "header.header.message-id": 1
    },
    "name": "header.header.message-id_index",
    "partialFilterExpression": {
      "header.header.message-id": {
        "$exists": true,
        "$gt": "#1/1"
      }
    },
    "unique": true
  }

I’ve tested it and it works as specified.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top