如何在对象数组中排序 - CouchBase

时间:2016-10-07 07:00:54

标签: arrays database sorting couchbase n1ql

嗨,目前我想排序对象数组,我使用ARRAY_SORT函数,它将使用对象的第一个字段进行排序&如果每个对象具有相同的JSON结构,它都可以正常工作。如果数组中的一个元素具有不同的JSON结构,则结果不正确。

我使用的查询:

SELECT ARRAY_SORT(c.student) as student FROM Class c

结果:

"student": [
  {
    "id": 3,
    "name": "Kenny35"
  },
  {
    "id": 6,
    "name": "Kenny35"
  },
  {
    "id": 7,
    "name": "Kenny35"
  },
  {
    "id": 8,
    "name": "Kenny35"
  },
  {
    "hobby": "video game",
    "id": 5,
    "name": "Kenny35"
  }
]

如何为ARRAY_SORT函数指定数组中对象的属性?

2 个答案:

答案 0 :(得分:1)

dev的, 首先根据对象的长度/大小比较对象,然后是对象中的字段。 http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/comparisonops.html

这是现在支持的唯一排序规则。 -Prasad

答案 1 :(得分:0)

您可以发出查询并使用ORDER BY。

SELECT *
FROM Class c
UNNEST c.student s
ORDER BY ...