Closed noahehall closed 4 years ago
this indeed works in directly in the arango web interface query thing
LET doc = DOCUMENT("Paths/account-collectors")
let itemId = 'base'
let obj = { wtf: 'yo' }
let prop = 'strategies'
let updated = (
FOR item in doc[prop]
FILTER item.id == itemId
return MERGE(item, obj)
)
UPDATE doc WITH { [prop]: updated} in 'Paths'
RETURN doc
but fails when I use the node arango thing
if anyone else arrives here thinking wtf the solution was definitely thinking about the returned error.
in node you have to declare the variables within the aql template
working query:
aql`
let colName = ${collectionName} // <---
let key = ${thisDocKey} // <---
LET doc = DOCUMENT(colName,key) // <---
let itemId = ${itemId}
let obj = ${obj}
let prop = ${prop}
let updated = (
FOR item in doc[prop]
FILTER item.id == itemId
return MERGE(item, obj)
)
UPDATE doc WITH { [prop]: updated} in ${db.collection(collectionName)}
RETURN doc
`
is there a better way to do what im doing here? e.g.
also intriguingly this works elseware in my file
RETURN UNSET(DOCUMENT("${collectionName}/${thisDocKey}"), "_key", "_id", "_rev")
Why do I have to use the intermediate variables when doing the loop?
@jsteemann ^^
ps, always return NEW mf
ps read this https://www.arangodb.com/docs/3.6/aql/examples-data-modification-queries.html
in the loop you have to check each item MERGE only the matching item, else you'll rewrite the entire prop
hello arangotans
playing on the solution to this issue
problem: update an item, in an array, on a document error: ArangoError: AQL: collection or array expected as operand to FOR loop; you provided a value of type 'null'
sample document:
query throwing error
obviously its an error with arango, and not my query ;)