代码之家  ›  专栏  ›  技术社区  ›  Rob

Azure Comos-按空数组元素排序

  •  0
  • Rob  · 技术社区  · 6 年前

    我有一个具有数组属性的数据集。当我 select * from c :

    [
      {
        "id": "1",
        "props": []
      },
      {
        "id": "2",
        "props": ["a"]
      },
      {
        "id": "3",
        "props": ["b"]
      }
    ]
    

    select * from c order by c.props

    但是它返回0条记录,因为第一个数组项是空的(如果我按顺序删除,它可以正常工作并返回三条记录)。我可以这样做来尝试绕过它:

    select c.* as item, props = [] ? '' : props[0] as orderCol from c

    select c.* as item, props = [] ? '' : props[0] as orderCol from c order by c.orderCol

    它返回0条记录。我做错什么了?(我不想用where子句过滤空的props记录)。基本上,我不能按我的自定义选择字段排序 “订单列” 当我想的时候。

    注意:使用 order by c.id

    感谢您提前提出任何建议!

    0 回复  |  直到 6 年前
        1
  •  1
  •   Jay Gong    6 年前

    测试了你的方案并得到了同样的结果结果。它order by似乎可以映射到文档路径,并且不能包含未定义的列。我建议您使用以下sql:

    select c.id,IS_DEFINED(c.props[0]) ? c.props[0]:"" as propsVal from c
    

    输出:

    enter image description here

    然后通过编码对结果进行排序,例如 Array.Sort() 方法。

    推荐文章