Hi Ryan!
Index entries are not stored if they are null
– that is, if the entire entry resolves to null
Explanation
In the article, the Index is defined by two values
. That means that each index entry is an array of values, not a single value. null
is permitted to be one of those values. If null
is present, sorting follows the precedence specified in the docs.
In your shared example, by only defining one value, each entry in the index is stored as is. The index will not store entries if they resolve to null
– they will be effectively skipped.
To try to clarify with data, in the docs for index sorting, the index page looks like:
{
data: [
[11, Ref(Collection("Letters"), "111")],
[24, Ref(Collection("Letters"), "124")],
["", Ref(Collection("Letters"), "112")],
["14th", Ref(Collection("Letters"), "114")],
["16th", Ref(Collection("Letters"), "116")],
["18th", Ref(Collection("Letters"), "118")],
["19th", Ref(Collection("Letters"), "119")],
["20th", Ref(Collection("Letters"), "120")],
["21st", Ref(Collection("Letters"), "121")],
["22nd", Ref(Collection("Letters"), "122")],
["24 + 1", Ref(Collection("Letters"), "125")],
["4th", Ref(Collection("Letters"), "104")],
["9th", Ref(Collection("Letters"), "109")],
["eighth", Ref(Collection("Letters"), "108")],
["fifteenth", Ref(Collection("Letters"), "115")],
["fifth", Ref(Collection("Letters"), "105")],
["First", Ref(Collection("Letters"), "101")],
["second", Ref(Collection("Letters"), "102")],
["seventeenth", Ref(Collection("Letters"), "117")],
["seventh", Ref(Collection("Letters"), "107")],
["sixth", Ref(Collection("Letters"), "106")],
["tenth", Ref(Collection("Letters"), "110")],
["third", Ref(Collection("Letters"), "103")],
["twenty-third", Ref(Collection("Letters"), "123")],
[null, Ref(Collection("Letters"), "113")], // <-- null in the index entry as part of an array
[null, Ref(Collection("Letters"), "126")] // <-- null in the index entry as part of an array
]
}
Note that each entry is an array. The example in the docs uses Map
to extract just the first element, which could make it look like the index is storing a null
value on its own.
Workaround
To capture what you are trying to do, consider adding the ref
as a value to your index.
CreateIndex({
name: "index_name",
source: Collection("cars"),
terms: [
{ field: ["data", "orgId"] },
{ field: ["data", "car"] },
{ field: ["data", "verified"] }
],
values: [
{ field: ["data", "lastUsed"], reverse: true },
{ field: ["ref"] }
]
})
An aside about using null in Indexes
You can actually use this to your advantage in cases where you want to exclude some entries out from an index. Here is an example use case: