My documents are like this:
{ a: [ { u: 1 }, {} ] },
{ a: [ { u: 2 }, {}, {} ] },
{ a: [ { u: 3 } ] }
And I want to create unique index on a.u
if it exists:
{
key: { 'a.u': 1 } },
unique: true,
partialFilterExpression: { 'a.u': { $exists: true } }
}
The documents will be indexed like this:
u: 1 -> { a: [ { u: 1 }, {} ] }
u: 2 -> { a: [ { u: 2 }, {}, {} ] }
u: 3 -> { a: [ { u: 3 } ] }
u: null -> { a: [ { u: 1 }, {} ] }
u: null -> { a: [ { u: 2 }, {}, {} ] } <-- duplicated key
Here comes the problem, there is a a.u
in both records, so the partial filter expression matches both records. But both also have an empty element in the array with a.u
of null
, and I get the error of duplicated key because of this null
.
Is there any way that I can solve this problem? Or I have to change the schema?