I have the following data from a json file
[
{
"_id": "5e7b0e1c2ff059dbea2bf2d3",
"gender": "female",
"outstandingloans": [
{
"id": 0,
"balance": -1438
},
{
"id": 1,
"balance": -1989.31
},
{
"id": 2,
"balance": -482.08
},
{
"id": 3,
"balance": -1142.74
}
]
}
]
The data is imported to neo4j from mongodb through apoc and I need to give id, gender and outstandingloans as properties to node created. I need to add all the balance amounts as properties of node. How can we do this? I was able to add only one value not others. The cypher command i used are
CALL apoc.mongodb.get('mongodb://localhost:27017','bankdata','crdf',null) yield value
MERGE (cr:creditdefault{id:value._id, gender:value.gender})
WITH cr, (value.outstandingloans) AS bal
UNWIND bal AS ob"
Thanks
Because you have a 1 -> Many setup here, where one creditdefault has many outstanding balances, you probably shouldn't add them as properties. Instead do something like this, as separate nodes.
CALL apoc.mongodb.get('mongodb://localhost:27017','bankdata','crdf',null) yield value
MERGE (cr:creditdefault{id:value._id, gender:value.gender})
WITH cr, (value.outstandingloans) AS bal
UNWIND bal AS obj
CREATE (ol:OutstandingLoan {
id: obj.id,
balance: obj.balance
})
MERGE (cr)-[:HAS_BALANCE]->(ol)
Thanks David.
In case I add them as nodes how can i sum them to see individual balance or balance based on gender. Is there sum function for nodes? I saw sum for properties that's why I opted to make them properties.
You should have a look at cypher language basics, but yes it's easy to sum them with a simple query, something like this:
MATCH (cr:creditdefault)-[:HAS_BALANCE]->(ol:OutstandingLoan)
RETURN distinct(cr.id), sum(ol.balance)
This would show you each creditdefault node and the total balance across all linked nodes.
Thanks David. I did not concentrate on your previous post about creating OutstandingLoan node with id and balance. Actually in this case too the property of OutstandingLoan is summed up. Thank you so much once again.
When I need to take average spending of male and female defaulters the number of defaulters are shown more since we unwinded the outstandingLoan. For each defaulter the if he/she has more than 1 loan then that defaulter is taken more than once. How can I overcome this problem? I tried but I am unable to get correct average.
Please post a new thread to address a different question -- and when you do, please paste what cypher code you've tried and what is has produced so far.
I got the suitable query for displaying average defaulters based on gender in the above problem. The query is:
MATCH (cr:creditdefault)-[:HAS_BALANCE]->(ol:OutstandingLoan)
WITH cr,sum(ol.balance) as bal
RETURN cr.gender as Gender, count(cr.gender) as GenderCount, sum(bal) as TotalBalance, avg(bal) as AverageBalance
Since I already got the required query, I think there is no need raising this as a new post.
Thanks a lot for your help.