DocumentDB Queries help/reference

时间:2016-08-31 18:10:56

标签: azure-cosmosdb

EDIT: I see downvote for this question, editing my question to add clarity and readability.

I'm from SQL background ramping up on Document DB Query syntax. I'm looking for document db equivalent syntax for the following T-SQL Queries. Any help appreciated.

  1. Select * from c where c.header.TimeStampField >= 'Time Stamp Constant'

  2. Select * from c where Upper(c.stringifield) = "lowercase to uppercase"

  3. select count(1) from c --Find count of all documents

Is it possible to delete the documents thru a query ? like RDBMS queries (delete from table?)

3 个答案:

答案 0 :(得分:1)

Your queries #1 and #2 should work exactly as specified.

Query #3 is not possible. However, the total size of documents is returned in the documentsCount property in the x-ms-resource-usage response header. That's a new feature that may or may not be supported by the SDK you are using though. So, you can get a count of all documents by going through all of the lines. Here is a fully worked example of doing it in a stored procedure. Alternatively, you could do SELECT 1 FROM c and bring back all of the 1's and count/sum them.

It is not possible to delete documents through a query like in SQL. You can do it in a sproc or use the client side functionality for delete document. In both cases, you'd need to first query the docs and then delete them one at a time.

答案 1 :(得分:1)

借助DocumentDB的最新聚合支持(2017年2月),您现在可以使用以下内容生成计数:

SELECT VALUE COUNT(1)
FROM mydocuments d

这也适用于过滤器。例如:

SELECT VALUE COUNT(1)
FROM mydocuments d
WHERE d.foo = "bar"

还支持最小/最大/平均/总和。

答案 2 :(得分:0)

  1. 您的查询已经适用于DocumentDB。
  2. 您的查询是正确的,应该有效。但是,另一个优化方法是在同一文档中存储一个单独的属性,该文档具有所有大写并明确搜索它。此更改将使用索引而不是扫描。
  3. Larry对#3的回答是正确的方法。
  4. 要进一步深入了解查询语言,您可以使用playground @ https://www.documentdb.com/sql/demo