mongo-java-server icon indicating copy to clipboard operation
mongo-java-server copied to clipboard

Partial Filter Expressions on unique indexes are unsupported?

Open ghost opened this issue 5 years ago • 2 comments

I have a collection that contains heterogeneous documents. Some documents are uniquely identified by the value of field a (and do not contain field b) and the others are uniquely identified by field b (and do not contain field a).

For example:

{ "a": "foo", "value": "qux" }
{ "b": 1, "value": "quux" }

I have created indexes with the following definitions:

[
  {
    "key": {
      "_id": 1
    },
    "name": "_id_",
    "ns": "my.collection",
    "v": 2
  },
  {
    "key": {
      "a": 1
    },
    "name": "aUniqueIndex",
    "ns": "my.collection",
    "unique": true,
    "partialFilterExpression": {
      "a": {
        "$exists": true
      }
    },
    "v": 2
  },
  {
    "key": {
      "b": 1
    },
    "name": "bUniqueIndex",
    "ns": "my.collection",
    "unique": true,
    "partialFilterExpression": {
      "b": {
        "$exists": true
      }
    },
    "v": 2
  }
]

Inserting the following documents:

{ "a": "foo", "value": "qux" }
{ "b": 1, "value": "quux" }
{ "a": "bar", "value": "quuux" }

... results in a unique constraint violation on bUniqueIndex where the unique value being violated is b: null

These documents should not be present in bUniqueIndex. I looked through the code and could find no mention of partial filter expressions on unique indexes, leading me to believe that they are unsupported at this time.

ghost avatar Sep 16 '20 22:09 ghost

It seems that it does not support partial index.

msl12 avatar Jun 30 '21 05:06 msl12

It’s not yet implemented. I’m happy to receive a PR.

bwaldvogel avatar Jun 30 '21 14:06 bwaldvogel