DocumentDB - WHERE clause within collection

一个人想着一个人 提交于 2019-12-23 16:36:48

问题


Given a sample document like this one from the Microsoft examples:

{
  "id": "AndersenFamily",
  "lastName": "Andersen",
  "parents": [
     { "firstName": "Thomas" },
     { "firstName": "Mary Kay"}
  ],
  "children": [
     {
         "firstName": "Henriette Thaulow", 
         "gender": "female", 
         "grade": 5,
         "pets": [{ "givenName": "Fluffy" }]
     }
  ],
  "address": { "state": "WA", "county": "King", "city": "seattle" },
  "creationDate": 1431620472,
  "isRegistered": true
}

We can see that there is a sub-collection children containing an array of documents.

Let's say I wanted to write a query using the SELECT ... FROM ... WHERE ... type syntax, how would I go about writing a query to find families with any daughters (any children with gender "female")

So something like

SELECT c.id
FROM c
WHERE c.children.contains(  // I'm stuck!

I'm wondering if I'm missing a JOIN or something but honestly I'm not sure where I go from here, and I'm struggling to find anything helpful in Google partially because I'm not sure how to phrase my search!


回答1:


You need the JOIN keyword to unwind the children, then apply the filter on gender like the query below:

SELECT f.id
FROM family f
JOIN child IN f.children
WHERE child.gender = "female"


来源:https://stackoverflow.com/questions/42588163/documentdb-where-clause-within-collection

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