To access property name by a variable

Hello

I have two nodes with the following details:

1 Node - It is question node which has question Id and question Text as property
2 Node - It has questionId as property and keys are questions answers

So now I want to get return the question Text and their answers in one object.

I have written the following query:


MATCH(a:Answers) with keys(a) as questionIds,a
UNWIND questionIds as questionId
MATCH(q:Question{questionId:questionId}) 
RETURN collect({question:q.questionText,answer:a.questionId}) as questionanswers

Now how to return the answers as a.questionId is not allowed as questionId is not the property name it is the variable.

Thanks

Hello @utkarshgoyal55 :slight_smile:

Can you give an example of what you want to return?

Regards,
Cobra

Question Node - {questionId: "xfElaSuQ", questionText: "What are you doing?"}
AnswerNode - {xfElaSuQ: "This is the answer for the question"}

Return Node: {question: "What are you doing?", answer: "This is the answer for the question"}

Your first query is not solving the problem?

It is giving an error because "a.questionId" in this questionId is variable from the unwind function such property does not exist in the node

There is no relation in your model?

Yes there are relationships but all that are not related to the output which I required

MATCH (a:Answers) WITH a, keys(a) AS questionIds
UNWIND questionIds AS questionId
MATCH (q:Question{questionId:questionId}) 
RETURN collect({question:q.questionText, answer:a[questionId]}) AS questionanswers

Thanks, @cobra it works

1 Like