简体   繁体   中英

Is ArangoDB edge document right place to put additional key-value?

In MySql we create simple users friendships database with two models users and friendships.

Model friendships contain keys:

  • id
  • from (ref. key)
  • to (ref. key)
  • created
  • modified
  • approved (0,1)
  • block (0,1)

What is the correct way to make the same in ArangoDB, Can there be a "friendships" edge document with added key - values? Is it possible to perform update edge document? Also, is it possible these values show after performing the request as a result?

Give me an example.

First create a "users" collection and a "friendship" collection.

arangosh [_system]> db._create("users");
[ArangoCollection 33746098, "users" (type document, status loaded)]

arangosh [_system]> db._createEdgeCollection("friendship");
[ArangoCollection 33877170, "friendship" (type edge, status loaded)]

Create two users

arangosh [_system]> db.users.save({_key: "you"});
{ 
  "_id" : "users/you", 
  "_rev" : "34598066", 
  "_key" : "you" 
}

arangosh [_system]> db.users.save({_key: "me"});
{ 
  "_id" : "users/me", 
  "_rev" : "34794674", 
  "_key" : "me" 
}

and a relationship between them

arangosh [_system]> db.friendship.save("users/me", "users/you", {});
{ 
  "_id" : "friendship/35515570", 
  "_rev" : "35515570", 
  "_key" : "35515570" 
}

Check the users

arangosh [_system]> db._query("FOR u IN users RETURN u").toArray()
[ 
  { 
    "_id" : "users/me", 
    "_rev" : "34794674", 
    "_key" : "me" 
  }, 
  { 
    "_id" : "users/you", 
    "_rev" : "34598066", 
    "_key" : "you" 
  } 
]

and their neighbors

arangosh [_system]> db._query("FOR u IN users RETURN { user: u, friends: NEIGHBORS(users, friendship, u, 'outbound') }").toArray()
[ 
  { 
    "user" : { 
      "_id" : "users/me", 
      "_key" : "me", 
      "_rev" : "34794674" 
    }, 
    "friends" : [ 
      "users/you" 
    ] 
  }, 
  { 
    "user" : { 
      "_id" : "users/you", 
      "_key" : "you", 
      "_rev" : "34598066" 
    }, 
    "friends" : [ ] 
  } 
]

add "block"

arangosh [_system]> db._query("FOR f IN friendship FILTER f._from == 'users/me' and f._to == 'users/you' UPDATE f WITH { block: true } IN friendship RETURN { before: OLD, after: NEW }").toArray()
[ 
  { 
    "before" : { 
      "_id" : "friendship/35515570", 
      "_rev" : "35515570", 
      "_key" : "35515570", 
      "_from" : "users/me", 
      "_to" : "users/you" 
    }, 
    "after" : { 
      "block" : true, 
      "_id" : "friendship/35515570", 
      "_rev" : "63892658", 
      "_key" : "35515570", 
      "_from" : "users/me", 
      "_to" : "users/you" 
    } 
  } 
]

The return will return the whole document. You can also only return the block attribute

arangosh [_system]> db._query("FOR f IN friendship FILTER f._from == 'users/me' and f._to == 'users/you' UPDATE f WITH { block: false } IN friendship RETURN { before: OLD.block, after: NEW.block }").toArray()
[ 
  { 
    "before" : true, 
    "after" : false 
  } 
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM