We have a collection "packages" like the following:
| package | version | released |
| --------------------------- | ------- | -------- |
| 'https://example.com/a.pkg' | '1.0.0' | true |
| 'https://example.com/b.pkg' | '2.0.0' | true |
| 'https://example.com/c.pkg' | '3.0.0' | true |
| 'https://example.com/d.pkg' | '4.0.0' | true |
Now, we need to find the highest version. So we write down the code:
find({ released: true }).sort({ version: -1 }).limit(1)
But, quickly, we find it does not work. Since sort
uses string comparison, it is not what we expect. What we expect is version comparison, which is a customized function (javascript).
Finally, the question is: how to write customized comparator function?