Erro ao criar índice com valor nulo #2524
-
Versão laravel: 9 Estou tentando criar um índice único, segue meu código:
Porém mesmo utilizando sparse para ignorar registros como valor nulo, é retornado o seguinte erro:
Alguém poderia me ajudar? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The
In this case, you should be using partial indexes. These offer a superset of the sparse index functionality by letting you define your own criteria for including a document in the index. However, the $options = [
'unique' => true,
'partialFilterExpression' => [
'numRps' => ['$ne' => null],
'serieRps' => ['$ne' => null],
],
];
$collection->index(['company_id', 'numRps', 'serieRps'], 'uniques', null, $options); Creating this index will yield the following error (note that
Depending on the type of the values in the two fields you can work around this by using the $options = [
'unique' => true,
'partialFilterExpression' => [
'numRps' => ['$gt' => 0],
'serieRps' => ['$type' => 'string'],
],
];
$collection->index(['company_id', 'numRps', 'serieRps'], 'uniques', null, $options); |
Beta Was this translation helpful? Give feedback.
The
sparse
index option doesn't exclude documents where a single field in a compound index isnull
. From the documentation:In this case, you should be using partial indexes. These offer a superset of the sparse index functionality by letting you define your own criteria for including a document in the index. However, the
partialFilterExpression
only supports a subset of the usual filter criteria. In this case, you'd be looking to exclude any document wherenumRps
orserieRps
is notnull
. Usually, this would result in this filter (note that the example does not work):