Definição
$indexOfBytesPesquisa uma string para uma ocorrência de uma substring e retorna o índice de bytes UTF-8 (com base em zero) da primeira ocorrência. Se a substring não for encontrada, retorna
-1.$indexOfByteshas the following operator expression syntax:{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] } OperandoDescrição<string expression>Pode ser qualquer expressão válida desde que resolva para uma string. Para mais informações sobre expressões, consulte Expressões.
If the string expression resolves to a value of
nullor refers to a field that is missing,$indexOfBytesreturnsnull.If the string expression does not resolve to a string or
nullnor refers to a missing field,$indexOfBytesreturns an error.<substring expression>Pode ser qualquer expressão válida desde que resolva para uma string. Para mais informações sobre expressões, consulte Expressões.
<start>Opcional Um número integral que especifica a posição do índice inicial para a Atlas Search. Pode ser qualquer expressão válida que resolva um número integral não negativo.
<end>Optional An integral number that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a
<end>index value, you should also specify a<start>index value; otherwise,$indexOfBytesuses the<end>value as the<start>index value instead of the<end>value.
Comportamento
If
<string expression>is null,$indexOfBytesreturnsnull.If
$indexOfBytesis called on a field that doesn't exist in the document,$indexOfBytesreturnsnull.If
<string expression>is not a string and not null,$indexOfBytesreturns an error.If
<substring expression>is null,$indexOfBytesreturns an error.If
<start>or<end>is a negative number,$indexOfBytesreturns an error.If
<start>is a number greater than<end>,$indexOfBytesreturns-1.If
<start>is a number greater than the byte length of the string,$indexOfBytesreturns-1.If
<start>or<end>is given a value that is not an integer,$indexOfBytesreturns an error.If the
<substring expression>is found multiple times within the<string expression>, then$indexOfBytesreturns the index of the first<substring expression>found.
Alguns exemplos curtos para destacar comportamentos diferentes:
Exemplo | Resultados |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Exemplos
Considere uma coleção inventory com os seguintes documentos:
db.inventory.insertMany( [ { _id: 1, item: "foo" }, { _id: 2, item: "fóofoo" }, { _id: 3, item: "the foo bar" }, { _id: 4, item: "hello world fóo" }, { _id: 5, item: null }, { _id: 6, amount: 3 } ] )
The following operation uses the $indexOfBytes operator to retrieve the indexes at which the string foo is located in each item:
db.inventory.aggregate( [ { $project: { byteLocation: { $indexOfBytes: [ "$item", "foo" ] }, } } ] )
A operação retorna os seguintes resultados:
{ _id: 1, byteLocation: "0" } { _id: 2, byteLocation: "4" } { _id: 3, byteLocation: "4" } { _id: 4, byteLocation: "-1" } { _id: 5, byteLocation: null } { _id: 6, byteLocation: null }
Dica