DocumentDB SQL - Klausa Dimana
Dalam bab ini, kita akan membahas klausa WHERE, yang juga opsional seperti klausa FROM. Ini digunakan untuk menentukan kondisi saat mengambil data dalam bentuk dokumen JSON yang disediakan oleh sumber. Setiap dokumen JSON harus mengevaluasi kondisi yang ditentukan menjadi "benar" agar dipertimbangkan untuk hasil. Jika kondisi yang diberikan terpenuhi, baru kemudian mengembalikan data tertentu dalam bentuk dokumen JSON. Kita dapat menggunakan klausa WHERE untuk memfilter catatan dan hanya mengambil catatan yang diperlukan.
Kami akan mempertimbangkan tiga dokumen yang sama dalam contoh ini. Berikut ini adalahAndersenFamily dokumen.
{
"id": "AndersenFamily",
"lastName": "Andersen",
"parents": [
{ "firstName": "Thomas", "relationship": "father" },
{ "firstName": "Mary Kay", "relationship": "mother" }
],
"children": [
{
"firstName": "Henriette Thaulow",
"gender": "female",
"grade": 5,
"pets": [ { "givenName": "Fluffy", "type": "Rabbit" } ]
}
],
"location": { "state": "WA", "county": "King", "city": "Seattle" },
"isRegistered": true
}
Berikut ini adalah SmithFamily dokumen.
{
"id": "SmithFamily",
"parents": [
{ "familyName": "Smith", "givenName": "James" },
{ "familyName": "Curtis", "givenName": "Helen" }
],
"children": [
{
"givenName": "Michelle",
"gender": "female",
"grade": 1
},
{
"givenName": "John",
"gender": "male",
"grade": 7,
"pets": [
{ "givenName": "Tweetie", "type": "Bird" }
]
}
],
"location": {
"state": "NY",
"county": "Queens",
"city": "Forest Hills"
},
"isRegistered": true
}
Berikut ini adalah WakefieldFamily dokumen.
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 6,
"pets": [
{ "givenName": "Charlie Brown", "type": "Dog" },
{ "givenName": "Tiger", "type": "Cat" },
{ "givenName": "Princess", "type": "Cat" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 3,
"pets": [
{ "givenName": "Jake", "type": "Snake" }
]
}
],
"location": { "state": "NY", "county": "Manhattan", "city": "NY" },
"isRegistered": false
}
Mari kita lihat contoh sederhana di mana klausa WHERE digunakan.
Dalam kueri ini, di klausa WHERE, kondisi (WHERE f.id = "WakefieldFamily") ditentukan.
SELECT *
FROM f
WHERE f.id = "WakefieldFamily"
Ketika kueri di atas dijalankan, itu akan mengembalikan dokumen JSON lengkap untuk WakefieldFamily seperti yang ditunjukkan pada output berikut.
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
}
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 6,
"pets": [
{
"givenName": "Charlie Brown",
"type": "Dog"
},
{
"givenName": "Tiger",
"type": "Cat"
},
{
"givenName": "Princess",
"type": "Cat"
}
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 3,
"pets": [
{
"givenName": "Jake",
"type": "Snake"
}
]
}
],
"location": {
"state": "NY",
"county": "Manhattan",
"city": "NY"
},
"isRegistered": false,
"_rid": "Ic8LAJFujgECAAAAAAAAAA==",
"_ts": 1450541623,
"_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgECAAAAAAAAAA==/",
"_etag": "\"00000500-0000-0000-0000-567582370000\"",
"_attachments": "attachments/"
}
]