DGraph relationships without duplicate nodes

Hi, I try create database via dgraph, .net driver and JSON. I have 3 cities and distances between them in my list. I would like one complete graph without duplicates but finally I’ve got 3 different graphs (with nodes and edges ofc). Can I do one big complete graph?

My JSON looks sth like this:


[
{"ListOfDistances":[{"Distance":10,"id":2, "City": "city2"},{"Distance":246,"id":3, "City": "city3"}],
"Id": 123213,
"City": "City1"},
{"ListOfDistances":[{"Distance":10,"id":1, "City": "city1"},{"Distance":246,"id":3, "City": "city3"}],
"Id": 123213,
"City": "City2"},
{"ListOfDistances":[{"Distance":10,"id":1, "City": "city1"},{"Distance":246,"id":2, "City": "city2"}],
"Id": 123213,
"City": "City3"}
]

Thank You.

1 Like

Hi @BFine9,

I see that your data is represented very much like an adjacency list. And there is no way for dgraph to know that the item in the list is corresponding to some other node. So you are getting three different graphs.

I would rather suggest to not represent the data as adjacency list. You can represent it like this:

_:city1 <connects> _:city2 (distance=10) .  
_:city1 <connects> _:city3 (distance=246) .
_:city2 <connects> _:city1 (distance=10) .
_:city2 <connects> _:city3 (distance=246) .
_:city3 <connects> _:city1 (distance=246) .
_:city3 <connects> _:city3 (distance=246) .

Or rather than having edges in both direction you can even have an edge in just one direction and use @reverse directive. That completely depends on your usecase.

Let me know what do you think about this kind of representation.

2 Likes

Thank You!
I changed my code but am creating edges between two cities now.
I corrected my json and it’s look like:

[
 {
"City": "city1",
"Population": 1000,
"Distance": 100
"ToCity": 
{
"City": "city2",
"Population": 2000
},
{
"City: "city1",
"Population": 1000,
"Distance": 200
"ToCity": 
{
"City": "city3",
"Population": 3000
}
},
(etc)
}

I don’t have one graph so I don’t know its fine. Do I lose something when all data isn’t connect?

I didn’t quite get your changes. Is the key “ToCity” has value as list of cities it connects?

No, you don’t lose any data depending on if it is connected or not. All the data persists, independent of connections.

1 Like