如何根据字段的值进行排序?(how to sorting according to the value of a field? mongo)

编程入门 行业动态 更新时间:2024-10-10 05:27:06
如何根据字段的值进行排序?(how to sorting according to the value of a field? mongo)

我有一组数据。 当我选择过滤条件时,我想查询所有数据并将符合筛选条件的值显示在查询结果的前面。

例如

[ {color: 'blue', name: 'four'}, {color: 'green', name: 'five'}, {color: 'red', name: 'one'}, {color: 'red', name: 'two'}, {color: 'red', name: 'three'} ]

当我选择color:red ,并限制4,我想获得数据

[ {color: 'red', name: 'one'}, {color: 'red', name: 'two'}, {color: 'red', name: 'three'}, {color .........}// the fourth of data are not concerned for now ]

当我选择color:blue ,并限制4,我想获得数据

[ {color: 'blue', name: 'four'}, {color ........}, {color ........}, {color .........}// now just care the first data ]

有一些功能来实现这一目标?

我的英语很差,我希望其意思很清楚。

不管怎样,谢谢!

I have a set of data. When I select the filter conditions, I want to query all the data and put the values that conform to the screening conditions to display in front of the query results.

for example

[ {color: 'blue', name: 'four'}, {color: 'green', name: 'five'}, {color: 'red', name: 'one'}, {color: 'red', name: 'two'}, {color: 'red', name: 'three'} ]

when i choose color:red, and limit 4, I want to get the data

[ {color: 'red', name: 'one'}, {color: 'red', name: 'two'}, {color: 'red', name: 'three'}, {color .........}// the fourth of data are not concerned for now ]

and when i choose color:blue, and limit 4, I want to get the data

[ {color: 'blue', name: 'four'}, {color ........}, {color ........}, {color .........}// now just care the first data ]

Have some function to achieve this?

My english is so poor, I hope the meaning is clear.

anyway, thanks!

最满意答案

在您的具体示例中,您可以这样写:

db.color.aggregate([{ $addFields : { "rank" : { // add a field called rank to all our documents $cond: { if: { $eq: [ "$color", "blue" ] }, // if the color value matches "blue" then: 0, // then we want items to be on the top else: 1 // else we want them to be in the second part } } } }, { $sort : {"rank" : 1} // sort ascending by our newly created "rank" field }])

如果您使用的是旧版本的MongoDB(<3.4),它不支持$ addFields ,您可以改为使用$ project,如下所示:

db.color.aggregate([{ $project : { "color": 1, // we want to retain the value of the color field "name": 1, // the same goes for the name field "rank" : { // add a field called rank to all our documents $cond: { if: { $eq: [ "$color", "blue" ] }, // if the color value matches "blue" then: 0, // then we want items to be on the top else: 1 // else we want them to be in the second part } } } }, { $sort : {"rank" : 1} // sort ascending by our newly created "rank" field }])

In your specific example you can write this:

db.color.aggregate([{ $addFields : { "rank" : { // add a field called rank to all our documents $cond: { if: { $eq: [ "$color", "blue" ] }, // if the color value matches "blue" then: 0, // then we want items to be on the top else: 1 // else we want them to be in the second part } } } }, { $sort : {"rank" : 1} // sort ascending by our newly created "rank" field }])

If you are using an older version of MongoDB (<3.4) that does not support $addFields yet you can instead resort to using $project instead like so:

db.color.aggregate([{ $project : { "color": 1, // we want to retain the value of the color field "name": 1, // the same goes for the name field "rank" : { // add a field called rank to all our documents $cond: { if: { $eq: [ "$color", "blue" ] }, // if the color value matches "blue" then: 0, // then we want items to be on the top else: 1 // else we want them to be in the second part } } } }, { $sort : {"rank" : 1} // sort ascending by our newly created "rank" field }])

更多推荐

本文发布于:2023-07-27 03:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   sorting   mongo   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!