I am adding this answer as I was looking for a solution and could not find it. This may not answer exactly this question, or maybe, but it will help many others like me.
Example. If a field with null is houseName and it is of type string , the solution could be this:
db.collectionName.createIndex( {name: 1, houseName: 1}, {unique: true, partialFilterExpression: {houseName: {$type: "string"}}} );
This will ignore the null values ββin the houseName field and still be unique.
source share