github.com/dgraph-io/dgraph@v1.2.8/wiki/content/tutorial-2/index.md (about) 1 +++ 2 title = "Get Started with Dgraph - Basic Operations" 3 +++ 4 5 **Welcome to the second tutorial of getting started with Dgraph.** 6 7 In the [previous tutorial]({{< relref "tutorial-1/index.md" >}}) of getting started, 8 we learned some of the basics of Dgraph. 9 Including how to run the database, add new nodes and predicates, and query them back. 10 11 {{% load-img "/images/tutorials/2/graph-1.jpg" "Graph" %}} 12 13 In this tutorial, we'll build the above Graph and learn more about operations using the UID (Universal Identifier) of the nodes. 14 Specifically, we'll learn about: 15 16 - Querying and updating nodes, deleting predicates using their UIDs. 17 - Adding an edge between existing nodes. 18 - Adding a new predicate to an existing node. 19 - Traversing the Graph. 20 21 You can see the accompanying video below. 22 23 {{< youtube 8TKD-FFBVgE >}} 24 25 ---- 26 27 First, let's create our Graph. 28 29 Go to Ratel's mutate tab, paste the mutation below in the text area, and click Run. 30 31 32 ```json 33 { 34 "set":[ 35 { 36 "name": "Michael", 37 "age": 40, 38 "follows": { 39 "name": "Pawan", 40 "age": 28, 41 "follows":{ 42 "name": "Leyla", 43 "age": 31 44 } 45 } 46 } 47 ] 48 } 49 ``` 50 51 {{% load-img "/images/tutorials/2/a-add-data.gif" "mutation-1" %}} 52 53 ## Query using UIDs 54 55 The UID of the nodes can be used to query them back. 56 The built-in function uid takes a list of UIDs as a variadic argument, so you can pass one (e.g. uid(0x1)) or as many as you need (e.g. uid(0x1, 0x2)). 57 58 It returns the same UIDs that were passed as input, no matter whether they exist in the database or not. 59 But the predicates asked will be returned only if both the UIDs and their predicates exist. 60 61 Let's see the `uid` function in action. 62 63 First, let's copy the UID of the node created for `Michael`. 64 65 Go to the query tab, type in the query below, and click Run. 66 ```graphql 67 { 68 people(func: has(name)) { 69 uid 70 name 71 age 72 } 73 } 74 ``` 75 76 Now, from the result, copy the UID of Michael's node. 77 78 {{% load-img "/images/tutorials/2/b-get-uid-1.png" "get-uid" %}} 79 80 In the query below, replace the placeholder `MICHAELS_UID` with the UID you just copied, and run the query. 81 82 ```graphql 83 { 84 find_using_uid(func: uid(MICHAELS_UID)){ 85 uid 86 name 87 age 88 } 89 } 90 ``` 91 92 {{% load-img "/images/tutorials/2/c-query-uid.png" "get_node_from_uid" %}} 93 94 *Note: `MICHAELS_UID` appears as `0x8` in the images. The UID you get on your machine might have a different value.* 95 96 You can see that the `uid` function returns the node matching the UID for Michael's node. 97 98 Refer to the [previous tutorial]({{< relref "tutorial-1/index.md" >}}) if you have questions related to the structure of the query in general. 99 100 ## Updating predicates 101 102 You can also update one or more predicates of a node using its UID. 103 104 Michael recently celebrated his 41st birthday. Let's update his age to 41. 105 106 Go to the mutate tab and execute the mutation. 107 Again, don't forget to replace the placeholder `MICHAELS_UID` with the actual UID of the node for `Michael`. 108 109 ```json 110 { 111 "set":[ 112 { 113 "uid": "MICHAELS_UID", 114 "age": 41 115 } 116 ] 117 } 118 ``` 119 120 We had earlier used `set` to create new nodes. 121 But on using the UID of an existing node, it updates its predicates, instead of creating a new node. 122 123 You can see that Michael's age is updated to 41. 124 125 ```graphql 126 { 127 find_using_uid(func: uid(MICHAELS_UID)){ 128 name 129 age 130 } 131 } 132 ``` 133 134 {{% load-img "/images/tutorials/2/d-update-check.png" "update check" %}} 135 136 Similarly, you can also add new predicates to an existing node. 137 Since the predicate `country` doesn't exist for the node for `Michael`, it creates a new one. 138 139 ```json 140 { 141 "set":[ 142 { 143 "uid": "MICHAELS_UID", 144 "country": "Australia" 145 } 146 ] 147 } 148 ``` 149 150 ## Adding an edge between existing nodes 151 152 You can also add an edge between existing nodes using their UIDs. 153 154 Let's say, `Leyla` starts to follow `Michael`. 155 156 We know that this relationship between them has to represented by creating the `follows` edge between them. 157 158 {{% load-img "/images/tutorials/2/graph-2.jpg" "Graph" %}} 159 160 First, let's copy the UIDs of nodes for `Leyla` and `Michael` from Ratel. 161 162 Now, replace the placeholders `LEYLAS_UID` and `MICHAELS_UID` with the ones you copied, and execute the mutation. 163 164 165 ```json 166 { 167 "set":[ 168 { 169 "uid": "LEYLAS_UID", 170 "follows": { 171 "uid": "MICHAELS_UID" 172 } 173 } 174 ] 175 } 176 ``` 177 178 ## Traversing the edges 179 180 Graph databases offer many distinct capabilities. `Traversals` are among them. 181 182 Traversals answer questions or queries related to the relationship between the nodes. 183 Hence, queries like, `who does Michael follow?` 184 are answered by traversing the `follows` relationship. 185 186 Let's run a traversal query and then understand it in detail. 187 188 ```graphql 189 { 190 find_follower(func: uid(MICHAELS_UID)){ 191 name 192 age 193 follows { 194 name 195 age 196 } 197 } 198 } 199 ``` 200 201 Here's the result. 202 203 {{% load-img "/images/tutorials/2/e-traversal.png" "traversal-result" %}} 204 205 206 The query has three parts: 207 208 - **Selecting the root nodes.** 209 210 First, you need to select one or more nodes as the starting point for traversals. 211 These are called the root nodes. 212 In the query above, we use the `uid()` function to select the node created for `Michael` as the root node. 213 214 215 - **Choosing the edge to be traversed** 216 217 You need to specify the edge to be traversed, starting from the selected root nodes. 218 And then, the traversal, travels along these edges, from one end to the nodes at the other end. 219 220 In our query, we chose to traverse the `follows` edge starting from the node for `Michael`. 221 The traversal returns all the nodes connected to the node for `Michael` via the `follows` edge. 222 223 224 - **Specify the predicates to get back** 225 226 Since Michael follows only one person, the traversal returns just one node. 227 These are `level-2` nodes. The root nodes constitute the nodes for `level-1`. 228 Again, we need to specify which predicates you want to get back from `level-2` nodes. 229 230 {{% load-img "/images/tutorials/2/j-explain.JPG" "get_node_from_uid" %}} 231 232 You can extend the query to make use of `level-2` nodes and traverse the Graph further and deeper. 233 Let's explore that in the next section. 234 235 #### Multi-level traversals 236 237 The first level of traversal returns people followed by Michael. 238 The next level of traversal further returns the people they in-turn follow. 239 240 This pattern can be repeated multiple times to achieve multi-level traversals. 241 The depth of the query increases by one as we traverse each level of the Graph. 242 That's when we say that the query is deep! 243 244 245 ```graphql 246 { 247 find_follower(func: uid(MICHAELS_UID)) { 248 name 249 age 250 follows { 251 name 252 age 253 follows { 254 name 255 age 256 } 257 } 258 } 259 } 260 ``` 261 262 {{% load-img "/images/tutorials/2/f-level-3-traverse.png" "level-3-query" %}} 263 264 Here is one more example from the extention of the last query. 265 266 ```graphql 267 { 268 find_follower(func: uid(MICHAELS_UID)) { 269 name 270 age 271 follows { 272 name 273 age 274 follows { 275 name 276 age 277 follows { 278 name 279 age 280 } 281 } 282 } 283 } 284 } 285 ``` 286 287 {{% load-img "/images/tutorials/2/g-level-4-traversal.png" "level 3" %}} 288 289 This query is really long! The query is four levels deep. 290 In other words, the depth of the query is four. 291 If you ask, isn't there an in-built function that makes multi-level deep queries or traversals easy? 292 293 The answer is Yes! 294 That's what the `recurse()` function does. 295 Let's explore that in our next section. 296 297 #### Recursive traversals 298 299 Recursive queries makes it easier to perform multi-level deep traversals. 300 They let you easily traverse a subset of the Graph. 301 302 With the following recursive query, we achieve the same result as our last query. 303 But, with a much better querying experience. 304 305 306 ```graphql 307 { 308 find_follower(func: uid(MICHAELS_UID)) @recurse(depth: 4) { 309 name 310 age 311 follows 312 } 313 } 314 ``` 315 316 In the query above, the `recurse` function traverses the graph starting from the node for `Michael`. 317 You can choose any other node to be the starting point. 318 The depth parameter specifies the maximum depth the traversal query should consider. 319 320 Let's run the recursive traversal query after replacing the placeholder with the UID of node for Michael. 321 322 {{% load-img "/images/tutorials/2/h-recursive-traversal.png" "recurse" %}} 323 324 325 [Check out the docs](https://docs.dgraph.io/query-language/#recurse-query) for detailed instructions on using the `recurse` directive. 326 327 #### Edges have directions 328 329 Edges in Dgraph have directions. 330 331 For instance, the `follows` edge emerging from the node for `Michael`, points at the node for `Pawan`. 332 They have a notion of direction. 333 334 Traversing along the direction of an edge is natural to Dgraph. 335 We'll learn about traversing edges in reverse direction in our next tutorial. 336 337 ## Deleting a predicate 338 339 Predicates of a node can be deleted using the `delete` mutation. 340 Here's the syntax of the delete mutation to delete any predicate of a node, 341 342 ```graphql 343 { 344 delete { 345 <UID> <predicate_name> * . 346 } 347 } 348 ``` 349 350 Using the mutation syntax above, let's compose a delete mutation. 351 Let's delete the `age` predicate of the node for `Michael`. 352 353 ```graphql 354 { 355 delete { 356 <MICHAELS_UID> <age> * . 357 } 358 } 359 ``` 360 361 {{% load-img "/images/tutorials/2/i-delete.png" "recurse" %}} 362 363 ## Wrapping up 364 365 In this tutorial, we learned about the CRUD operations using UIDs. 366 We also learned about `recurse()` function. 367 368 Before we wrap, here's a sneak peek into our next tutorial. 369 370 Did you know that you could search predicates based on their value? 371 372 Sounds interesting? 373 374 Check out our next tutorial of the getting started series [here]({{< relref "tutorial-3/index.md" >}}). 375 376 ## Need Help 377 378 * Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, feature requests and discussions. 379 * Please use [Github Issues](https://github.com/dgraph-io/dgraph/issues) if you encounter bugs or have feature requests. 380 * You can also join our [Slack channel](http://slack.dgraph.io).