Sort query by content (not by order)

放肆的年华 提交于 2019-12-24 14:44:58

问题


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?

来源:https://stackoverflow.com/questions/24757433/sort-query-by-content-not-by-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!