I am trying to sort my mongodb query according to the occurence of a string in one of its fields (an array). So given this example dataset:
id | hits | categories
------------------------------
1 | 18 | ['sports']
2 | 12 | ['sports', 'news']
3 | 22 | []
4 | 20 | ['news']
I would like to make a query like this one ($includes
is obviously a made-up keyword):
Records.find({}, {sort: {$includes: {categories: 'news'}, hits: -1});
Which would give me the resulting output:
id | hits | categories
------------------------------
4 | 20 | ['news']
2 | 12 | ['sports', 'news']
3 | 22 | []
1 | 18 | ['sports']
Is there a simple way to do that?