Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to sort by index that is not at field position 0 #5

Closed
hexadecible opened this issue Jan 19, 2022 · 1 comment
Closed

Unable to sort by index that is not at field position 0 #5

hexadecible opened this issue Jan 19, 2022 · 1 comment

Comments

@hexadecible
Copy link
Contributor

It appears that if you create a new index with an array of fields, and then try to sort that collection by any field that is not at index 0, the query will break.

await db.collection('example').createIndex(['createdAt', 'example'])

await db.collection('example').insert({ example: 1, createdAt: new Date() })
await db.collection('example').insert({ example: 2, createdAt: new Date() })
await db.collection('example').insert({ example: 3, createdAt: new Date() })

const docs1 = await db.collection('example').find().sort('createdAt', -1) // Works
const docs2 = await db.collection('example').find().sort('example', -1) // This does not work
@RangerMauve
Copy link
Owner

Sadly, that's a limitation of how B+ trees and indexes work and is by design. You can't sort by a field that isn't at index 0 because the B+ tree only sorts by the prefix of the key. If there are other fields you need to sort them by then you'll need to have more indexes. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants