Sort by a specific array index
When creating indexes and sorting with "normal" queries, it is possible to index/sort on a specific array index (e.g. foo.0.bar
). I do this because the search results grid in our UI only has room to show the first foo.bar
result, so it doesn't make sense visually to sort by additional objects which the user can't see.
Since the new Atlas Search Sort syntax looks the same, I was hoping to do that with it as well, but it doesn't seem possible.
Example:
[
{ "xyzzy": "xyzzy",
"foo": [ { "bar": "A" } ] },
{ "xyzzy": "xyzzy",
"foo": [ { "bar": "B" }, { "bar": "D" } ] },
{ "xyzzy": "xyzzy",
"foo": [ { "bar": "C" } ] }
]
{
index: "myindex",
returnStoredSource: true,
phrase: { path: "xyzzy", query: "xyzzy" },
sort: { "foo.0.bar": -1 }
}
3
votes
David
shared this idea
-
David commented
While this feature would be valuable for me, I've had a better idea that would solve it and all kinds of other scenarios as well: