0

我有一个存储在 Azure Cosmos DB 中的 JSON 对象,并且我正在查看是否有一种方法可以编写可行的查询来执行基本的操作,例如 Order By。

结构看起来像:

[ 
   { 
      "id":"id1",
      "title":"test title",
      "dataRecord":{ 
         "version":1,
         "dataRecordItems":[ 
            { 
               "itemTitle":"item title 1",
               "type":"string",
               "value":"My First Title"
            },
            { 
               "itemTitle":"item number",
               "type":"number",
               "value":1
            },
            { 
               "itemTitle":"date",
               "type":"date",
               "value":"21/11/2019 00:00:00"
            }
         ]
      }
   },
   { 
      "id":"id2",
      "title":"test title again",
      "dataRecord":{ 
         "version":1,
         "dataRecordItems":[ 
            { 
               "itemTitle":"item title 2",
               "type":"string",
               "value":"My Second Title"
            },
            { 
               "itemTitle":"item number",
               "type":"number",
               "value":2
            },
            { 
               "itemTitle":"date",
               "type":"date",
               "value":"20/11/2019 00:00:00"
            }
         ]
      }
   ]

我可以使用 ARRAY_CONTAINS 来查找具有特定值的对象,但是如果我尝试按标题为“日期”的对象的值进行排序,则会遇到各种问题。

所以,作为一个例子,我想能够说类似(这里的伪代码):

SELECT  * FROM c WHERE
    ARRAY_CONTAINS(c.dataRecord.dataRecordItems, 
    {"itemTitle":"item title 2", "value" : "My Second Title"}, true)
    AND
    ARRAY_CONTAINS(c.dataRecord.dataRecordItems,{"itemTitle":"item number", "value" : 2}, true)
    ORDER BY < *** SOMEHOW GET THE DATE HERE from itemTitle = date ***

然后,在这个简单的情况下,我会返回所有内容,但按日期排序。

显然,将来我会退出个别领域,但如果我不能做第一部分,那一切都没有实际意义。

只是想知道是否有人有什么好主意。

干杯!

4

1 回答 1

0

您需要以 ISO 8601 格式存储日期:

Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)

https://www.w3.org/TR/NOTE-datetime

于 2019-10-21T01:20:48.347 回答