KeyStrings and array sort order

18 views
Skip to first unread message

Martin Häusler

unread,
Oct 25, 2022, 12:08:04 PM10/25/22
to wiredtiger-users
Hello everyone,

I'm sorry if this question is a bit off-topic and too far on the MongoDB-side of things, but it has come up in prior conversations on this mailing list.

I'm curious about the KeyStrings used for secondary indexing. The way I understand them (please correct me if I'm wrong) is that they are a different serialization of BSON objects such that the lexicographical comparison of two resulting byte arrays always produces the same result as the comparison of the actual BSON objects. To achieve that, they use a suffix encoding for their type information.

The MongoDB Manual Page for BSON Ordering states the following about comparing two arrays: 
  • A less-than comparison, or an ascending sort, compares the smallest elements of the array according to the BSON type sort order.

  • A greater-than comparison, or a descending sort, compares the largest elements of the array according to the reverse BSON type sort order.

This makes sense from a certain point of view, but it also means that the ascending sort order of documents is not necessarily the inverse of the descending sort order. For example:
  • Document 1: { "arr": [92, 18] }
  • Document 2: { "arr": [91, 19] }
If I sort them in ascending order according to the rule stated in the docs, I get [Document 1, Document 2] (because min(arr) in Document 1 is 18, and min(arr) in Document 2 is 19). If I sort them in descending order, I also get [Document 1, Document 2] (because max(arr) in Document 1 is 92, and max(arr) in Document 2 is 91).

If we have an index on the documents "arr" field, we will have to decide if we put the maximum or the minimum entry in our KeyString, right? Does that mean that this index can only be used to fetch the ascending sort order for the "arr" field, and has to be ignored when the user asks for descending sort order on "arr"?

The very purpose of sort keys and the sort definition stated in the docs seem very conflicting to me. Can anybody shed some light on what's actually going on under the hood here?

Thank you!

Martin

Keith Smith

unread,
Oct 25, 2022, 5:14:31 PM10/25/22
to wiredtig...@googlegroups.com
Hi Martin,

Unfortunately, your question is a bit beyond my knowledge of the MongoDB server. I'm not sure what expertise others on this list have but maybe someone else can help out. You might also try asking in the "Server Development" community on our UserVoice forum: https://www.mongodb.com/community/forums/c/other-mongodb-topics/server-dev/43

Keith

--
You received this message because you are subscribed to the Google Groups "wiredtiger-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wiredtiger-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wiredtiger-users/94e4ec1d-3200-45d1-b4eb-092235db5dden%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages