Discussion:
[arangodb-google] Appending values to list in AQL QUERY
d***@nextsteps.ai
2018-06-01 06:02:11 UTC
Permalink
Hi,

Could anyone help me in updating a list in document using AQL Query.

Example:-

{
"teacher":-"Ram"
"subjects_he_teach":[
{
"grade":9

"subjects_he_teaches":["physics","Maths"]
},
{
"grade":10

"subjects_he_teaches":["Telugu","Hindi"]
}
]

}

In the above document i need to append one more object of grade 8 and also
I need to update subjects he teaches in grade 9.


Could anyone help me regarding this.
--
You received this message because you are subscribed to the Google Groups "ArangoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arangodb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dharmateja Tummala
2018-06-01 09:04:50 UTC
Permalink
Thank you a lot for your response.
{
"_id": "teachers/ram",
"teacher": "Ram",
"subjects_he_teach": [
{
"grade": 9,
"subjects_he_teaches": [
"physics",
"maths"
]
},
{
"grade": 10,
"subjects_he_teaches": [
"Telugu",
"Hindi"
]
}
]
}
LET doc = DOCUMENT("teachers/ram")
LET new_subjects = (
FOR s IN doc.subjects_he_teach
// append to subjects_he_teaches in the group with grade == 9,
otherwise return original value
RETURN s.grade == 9 ? MERGE(s, { "subjects_he_teaches": APPEND(s.
subjects_he_teaches, ["chemistry", "biology"]) }) : s
)
// merge updated subjects with main document
LET doc1 = MERGE(doc, { "subjects_he_teach": new_subjects})
// add another subject/grade object to the **updated** document
LET doc2 = PUSH(doc1.subjects_he_teach, { "grade": 8,
"subjects_he_teaches": ["maths"] })
UPDATE doc2 IN teachers RETURN NEW
A possible change you could make to your data model is to use attribute
keys like grade9, grade10 etc.
{
"_id": "teachers/ram",
"teacher": "Ram",
"subjects_he_teach": {
"grade9": ["physics", "maths"],
"grade10": ["Telugu", "Hindi"]
}
}
LET doc = DOCUMENT("teachers/ram")
LET doc1 = MERGE_RECURSIVE(doc, { "subjects_he_teach": { "grade9": APPEND(
doc.subjects_he_teach.grade9, ["chemistry", "biology"]) } } )
LET doc2 = MERGE_RECURSIVE(doc1, { "subjects_he_teach": { "grade8": [
"maths" ] } } )
UPDATE doc2 IN teachers
--
You received this message because you are subscribed to the Google Groups "ArangoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "ArangoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arangodb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...