Cosmos DB queries - using ORDER BY when a property does not exist in all documents

若如初见. 提交于 2020-01-23 16:44:26

问题


We are experiencing an issue in when writing queries for Cosmos Document DB and we want to create a new document property and use it in an ORDER BY clause

If, for example, we had a set of documents like:

{
  "Name": "Geoff",
  "Company": "Acme"
},
{
  "Name": "Bob",
  "Company": "Bob Inc"
}

...and we write a query like SELECT * FROM c ORDER BY c.Name this works fine and returns both documents

However, if we were to add a new document with an additional property:

{
  "Name": "Geoff",
  "Company": "Acme"
},
{
  "Name": "Bob",
  "Company": "Bob Inc"
},
{
  "Name": "Sarah",
  "Company": "My Company Ltd",
  "Title": "President"
}

...and we write a query like SELECT * FROM c ORDER BY c.Title it will only return the document for Sarah and excludes the 2 without a Title property.

This means that the ORDER BY clause is behaving like a filter rather than just a sort, which seems unexpected.

It seems that all document schemas are likely to add properties over time. Unless we go back and add these properties to all existing document records in the container then we can never use them in an ORDER BY clause without excluding records.

Does anyone have a solution to allow the ORDER BY to only effect the Sort order of the result set?


回答1:


Currently, ORDER BY works off of indexed properties, and missing values are not included in the result of a query using ORDER BY.

As a workaround, you could do two queries and combine the results:

  • The current query you're doing, with ORDER BY, returning all documents containing the Title property, ordered
  • A second query, returning all documents that don't have Title defined.

The second query would look something like:

SELECT * FROM c
WHERE NOT IS_DEFINED(c.Title)

Also note that, according to this note within the EF Core repo issue list, behavior is a bit different when using compound indexes (where documents with missing properties are returned).



来源:https://stackoverflow.com/questions/59070915/cosmos-db-queries-using-order-by-when-a-property-does-not-exist-in-all-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!