I have a Activity Doc which keeps track of users activity’s and goals etc and I want to be able to get only the name and value of a key and date. So in my query I want to provide the date, the key and the userid. The blow gets me the correct Data.
SELECT d.date,
k.`key`,
k.name,
k.`value`
FROM Contacts c
UNNEST c.Data.daily AS d
UNNEST d.metrics AS k
WHERE c._type = "activity_dashboard"
AND d.date = "2021-11-01T00:00:00Z"
AND c.userid = "8D6D24A5-D669-45DC-99AC-F257BDA133A4"
AND k.`key` = 'c28f7ead-d87b-4ad5-b6b3-1f204b013b50'
The question is, is this the most effective way to do so or is there a better way. Also how would i go about if i want to update the value of this key ? Since UNNEST does not work in updates.
Below is some sample DATA
{
"_id" : "1fb898be-6438-4e8c-8eeb-c74745a73683",
"_type" : "activity_dashboard",
"userid" : "8D6D24A5-D669-45DC-99AC-F257BDA133A4",
"Data": {
"daily" : [
{ "date" : "2021-11-01T00:00:00Z",
"metrics" : [
{"key" : "8646ec5d-7a72-49bd-9a68-cf326d1c4a14", "name" : "Calls Made", "value" : 0},
{"key" : "c28f7ead-d87b-4ad5-b6b3-1f204b013b50", "name" : "Notes Written", "value" : 0},
{"key" : "d0181c74-22a9-4f99-9cc9-df3467c51805", "name" : "Pop-Bys Delivered", "value" : 0},
{"key" : "90d142ea-6748-4781-b2b9-4f05aab12956", "name" : "Database Additions", "value" : 0},
{"key" : "723e95dd-8c47-48ed-b9c3-1b010b092a1b", "name" : "Referrals Given", "value" : 0},
{"key" : "0f054686-ef13-4993-ac5b-f640ceeaaa8d", "name" : "Referrals Received", "value" : 0}
]
},
{ "date" : "2021-10-31T00:00:00Z",
"metrics" : [
{"key" : "8646ec5d-7a72-49bd-9a68-cf326d1c4a14", "name" : "Calls Made", "value" : 0},
{"key" : "c28f7ead-d87b-4ad5-b6b3-1f204b013b50", "name" : "Notes Written", "value" : 0},
{"key" : "d0181c74-22a9-4f99-9cc9-df3467c51805", "name" : "Pop-Bys Delivered", "value" : 0},
{"key" : "90d142ea-6748-4781-b2b9-4f05aab12956", "name" : "Database Additions", "value" : 0},
{"key" : "723e95dd-8c47-48ed-b9c3-1b010b092a1b", "name" : "Referrals Given", "value" : 0},
{"key" : "0f054686-ef13-4993-ac5b-f640ceeaaa8d", "name" : "Referrals Received", "value" : 0}
]
}
]}
}