2017년 1월 6일 금요일

Edge CRUD

Edges

Edge 는 _from과 _to 속성을 항상 포함하는 특수한 document이다. 따라서 document 메소드를 edge에 사용할 수 있다.
edge-collection.edges(vertex)
edges 메소드는 vertex로부터 나가는 edge 혹은 vertex로 들어오는 edge를 모두 찾을 것이다.
edge-collection.edges(vertices)
vertices는 반드시 _id나 document의 array여야 한다.
arangosh> db._create("vertex");
[ArangoCollection 7372, "vertex" (type document, status loaded)]
arangosh> db._createEdgeCollection("relation");
[ArangoCollection 7374, "relation" (type edge, status loaded)]
arangosh> myGraph.v1 = db.vertex.insert({ name : "vertex 1" });
{ 
  "_id" : "vertex/7377", 
  "_key" : "7377", 
  "_rev" : "_USnx8ae---" 
}
arangosh> myGraph.v2 = db.vertex.insert({ name : "vertex 2" });
{ 
  "_id" : "vertex/7381", 
  "_key" : "7381", 
  "_rev" : "_USnx8ae--_" 
}
arangosh> myGraph.e1 = db.relation.insert(myGraph.v1, myGraph.v2,
........> { label : "knows"});
{ 
  "_id" : "relation/7384", 
  "_key" : "7384", 
  "_rev" : "_USnx8ae--A" 
}
arangosh> db._document(myGraph.e1);
{ 
  "_key" : "7384", 
  "_id" : "relation/7384", 
  "_from" : "vertex/7377", 
  "_to" : "vertex/7381", 
  "_rev" : "_USnx8ae--A", 
  "label" : "knows" 
}
arangosh> db.relation.edges(myGraph.e1._id);
[ ]
edge-collection.inEdges(vertex)
vertex로 들어오는(_to = vertex) 모든 edge를 찾는다. 
edge-collection.inEdges(vertices)
배열로 넘길 경우 _id나 document형태의 array여야 한다.
Examples
arangosh> db._create("vertex");
[ArangoCollection 7391, "vertex" (type document, status loaded)]
arangosh> db._createEdgeCollection("relation");
[ArangoCollection 7393, "relation" (type edge, status loaded)]
arangosh> myGraph.v1 = db.vertex.insert({ name : "vertex 1" });
{ 
  "_id" : "vertex/7396", 
  "_key" : "7396", 
  "_rev" : "_USnx8gG---" 
}
arangosh> myGraph.v2 = db.vertex.insert({ name : "vertex 2" });
{ 
  "_id" : "vertex/7400", 
  "_key" : "7400", 
  "_rev" : "_USnx8gG--_" 
}
arangosh> myGraph.e1 = db.relation.insert(myGraph.v1, myGraph.v2,
........> { label : "knows"});
{ 
  "_id" : "relation/7403", 
  "_key" : "7403", 
  "_rev" : "_USnx8gK---" 
}
arangosh> db._document(myGraph.e1);
{ 
  "_key" : "7403", 
  "_id" : "relation/7403", 
  "_from" : "vertex/7396", 
  "_to" : "vertex/7400", 
  "_rev" : "_USnx8gK---", 
  "label" : "knows" 
}
arangosh> db.relation.inEdges(myGraph.v1._id);
[ ]
arangosh> db.relation.inEdges(myGraph.v2._id);
[ 
  { 
    "_key" : "7403", 
    "_id" : "relation/7403", 
    "_from" : "vertex/7396", 
    "_to" : "vertex/7400", 
    "_rev" : "_USnx8gK---", 
    "label" : "knows" 
  } 
]
edge-collection.outEdges(vertex)
vertex에서 나가는 모든 edge를 가져온다.
edge-collection.outEdges(vertices)

Examples
arangosh> db._create("vertex");
[ArangoCollection 7411, "vertex" (type document, status loaded)]
arangosh> db._createEdgeCollection("relation");
[ArangoCollection 7413, "relation" (type edge, status loaded)]
arangosh> myGraph.v1 = db.vertex.insert({ name : "vertex 1" });
{ 
  "_id" : "vertex/7416", 
  "_key" : "7416", 
  "_rev" : "_USnx8lC---" 
}
arangosh> myGraph.v2 = db.vertex.insert({ name : "vertex 2" });
{ 
  "_id" : "vertex/7420", 
  "_key" : "7420", 
  "_rev" : "_USnx8lG---" 
}
arangosh> myGraph.e1 = db.relation.insert(myGraph.v1, myGraph.v2,
........> { label : "knows"});
{ 
  "_id" : "relation/7423", 
  "_key" : "7423", 
  "_rev" : "_USnx8lG--_" 
}
arangosh> db._document(myGraph.e1);
{ 
  "_key" : "7423", 
  "_id" : "relation/7423", 
  "_from" : "vertex/7416", 
  "_to" : "vertex/7420", 
  "_rev" : "_USnx8lG--_", 
  "label" : "knows" 
}
arangosh> db.relation.outEdges(myGraph.v1._id);
[ 
  { 
    "_key" : "7423", 
    "_id" : "relation/7423", 
    "_from" : "vertex/7416", 
    "_to" : "vertex/7420", 
    "_rev" : "_USnx8lG--_", 
    "label" : "knows" 
  } 
]
arangosh> db.relation.outEdges(myGraph.v2._id);
[ ]

ArangoDB info 메소드

Get the Version of ArangoDB

db._version()
Examples
arangosh> require("@arangodb").db._version();
3.1.7

Document CRUD - Delete

Remove

collection.remove(selector)
selector에 해당하는 document를 지운다. selector는 _id나 _key를 포함해야하며 collection에 존재하는 값이어야 한다.
remove 메소드가 실행 후, _id, _key, _rev를 포함한 document를 리턴한다.
selector에 _rev를 포함할 경우, _id나 _key에 해당하는 문서를 검색한 후, 해당문서의 _rev와 selector의 _rev를 비교한다. 다를 경우 에러가 발생한다.
collection.remove(selector, options)
아래와 같은 옵션을 줄 수도 있다.
  • waitForSync:
  • overwrite:
  • returnOld:
  • silent:
collection.remove(document-handle)
collection.remove(document-handle, options)
위와 같이 document _id로 삭제할 수 있다.
collection.remove(document-key)
collection.remove(document-handle, options)
_key로도 삭제가 가능하다.
collection.remove(selectorarray)
collection.remove(selectorarray,options)
한 번에 여러 selector로 대상을 삭제할 수 있다.
Examples
Remove a document:
arangosh> a1 = db.example.insert({ a : 1 });
{ 
  "_id" : "example/14754", 
  "_key" : "14754", 
  "_rev" : "_USnzp1W---" 
}
arangosh> db.example.document(a1);
{ 
  "_key" : "14754", 
  "_id" : "example/14754", 
  "_rev" : "_USnzp1W---", 
  "a" : 1 
}
arangosh> db.example.remove(a1);
{ 
  "_id" : "example/14754", 
  "_key" : "14754", 
  "_rev" : "_USnzp1W---" 
}
arangosh> db.example.document(a1);
[ArangoError 1202: document not found]
Remove a document with a conflict:
arangosh> a1 = db.example.insert({ a : 1 });
{ 
  "_id" : "example/14741", 
  "_key" : "14741", 
  "_rev" : "_USnzpzC---" 
}
arangosh> a2 = db.example.replace(a1, { a : 2 });
{ 
  "_id" : "example/14741", 
  "_key" : "14741", 
  "_rev" : "_USnzpzG---", 
  "_oldRev" : "_USnzpzC---" 
}
arangosh> db.example.remove(a1);
[ArangoError 1200: precondition failed]
arangosh> db.example.remove(a1, true);
{ 
  "_id" : "example/14741", 
  "_key" : "14741", 
  "_rev" : "_USnzpzG---" 
}
arangosh> db.example.document(a1);
[ArangoError 1202: document not found]

Remove By Keys

collection.removeByKeys(keys)
key 배열로 찾아 삭제한다. key에 해당하는 문서가 없을 경우 무시된다.
위 메소드는 "removed" 속성으로 삭제된 document의 수를, "ignored" 속성으로 무시된 key의 수를 리턴한다.
Examples
arangosh> keys = [ ];
[ ]
arangosh> for (var i = 0; i < 10; ++i) {
........>   db.example.insert({ _key: "test" + i, value: i });
........>   keys.push("test" + i);
........> }
arangosh> db.example.removeByKeys(keys);
{ 
  "removed" : 10, 
  "ignored" : 0 
}

Remove By Example

collection.removeByExample(example)
example에 해당하는 document를 삭제한다. 아래와 같이 순서대로 옵션을 줄 수 있다.
collection.removeByExample(document, waitForSync)
collection.removeByExample(document, waitForSync, limit)

Examples
arangosh> db.example.removeByExample( {Hello : "world"} );
1

Document CRUD - Update

Replace

collection.replace(selector, data)
이미 있는 document를 찾아서 수정한다. selector에 의해서 document를 선택하는데, selector는 object여야 하며, _id나 _key를 포함해야 한다. 그리고 이 _id나 _key값이 collection에 존재하는 값이어야 한다. 선택된 document는 두 번째 인자인 data로 교체된다. data의 _id, _key, _rev값은 모두 무시된다.
replace 메소드 호출 이후, _id, _key, _rev, _oldRev 를 포함한 document가 리턴된다. _oldRev는 replace 이전 _rev값이다.
만약 selector에 _rev 값이 명시된다면, _id, _key값으로 검색된 document의 현재 _rev값을 체크해서 만약 다르다면 충돌이 발생하고, 에러를 던지게 된다.
collection.replace(selector, data, options)
As before, but options must be an object that can contain the following boolean attributes:
위와 같이 옵션을 줄 수도 있다. option은 object여야 하며 아래 옵션이 있다.
  • waitForSync
  • overwrite: true이면, selector의 _rev값이 무시되어 revision이 다르더라도 덮어씌운다.
  • returnNew: true이면 replace 된 document를 리턴한다.
  • returnOld: true이면 replace 후 이전 document를 리턴한다.
  • silent: true이면 아무 값도 리턴하지 않는다.
collection.replace(document-handle, data)
collection.replace(document-handle, data, options)
selector 대신 _id로 검색할 수 있다.
collection.replace(document-key, data)
collection.replace(document-key, data, options)
selector 대신 _key로 검색할 수 있다.
collection.replace(selectorarray, dataarray)
collection.replace(selectorarray, dataarray, options)
selector 배열을 통해 한꺼번에 replace할 수 있다. selectorarray와 dataarray의 길이는 반드시 같아야 한다.
Examples
Create and update a document:
arangosh> a1 = db.example.insert({ a : 1 });
{ 
  "_id" : "example/14903", 
  "_key" : "14903", 
  "_rev" : "_USnzqcO---" 
}
arangosh> a2 = db.example.replace(a1, { a : 2 });
{ 
  "_id" : "example/14903", 
  "_key" : "14903", 
  "_rev" : "_USnzqcS---", 
  "_oldRev" : "_USnzqcO---" 
}
arangosh> a3 = db.example.replace(a1, { a : 3 });
[ArangoError 1200: precondition failed]
Use a document handle:
arangosh> a1 = db.example.insert({ a : 1 });
{ 
  "_id" : "example/14916", 
  "_key" : "14916", 
  "_rev" : "_USnzqfO--_" 
}
arangosh> a2 = db.example.replace("example/3903044", { a : 2 });
{ 
  "_id" : "example/3903044", 
  "_key" : "3903044", 
  "_rev" : "_USnzqfS---", 
  "_oldRev" : "_USnzqfO---" 
}

Update

collection.update(selector, data)
Replace와 동일하게 동작하지만, replace는 문서 전체를 갈아끼우고 update는 부분적으로 수정한다.
collection.update(selector, data, options)
 
Replace와 동일하게 동작하지만, replace는 문서 전체를 갈아끼우고 update는 부분적으로 수정한다. 몇 가지 옵션이 더 있다.
  • waitForSync: 
  • overwrite: 
  • returnNew: 
  • returnOld: 
  • silent: 
  • keepNull: false일 경우, data의 속성 값이 null인 것은 모두 제거된다.
  • mergeObjects: false일 경우 replace와 같다.
collection.update(document-handle, data)
collection.update(document-handle, data, options)
collection.update(document-key, data)
collection.update(document-key, data, options)
collection.update(selectorarray, dataarray)
collection.update(selectorarray, dataarray, options)
Replace와 동일하게 동작하지만, replace는 문서 전체를 갈아끼우고 update는 부분적으로 수정한다.
Examples
Create and update a document:
arangosh> a1 = db.example.insert({"a" : 1});
{ 
  "_id" : "example/14984", 
  "_key" : "14984", 
  "_rev" : "_USnzqre---" 
}
arangosh> a2 = db.example.update(a1, {"b" : 2, "c" : 3});
{ 
  "_id" : "example/14984", 
  "_key" : "14984", 
  "_rev" : "_USnzqri---", 
  "_oldRev" : "_USnzqre---" 
}
arangosh> a3 = db.example.update(a1, {"d" : 4});
[ArangoError 1200: precondition failed]
arangosh> a4 = db.example.update(a2, {"e" : 5, "f" : 6 });
{ 
  "_id" : "example/14984", 
  "_key" : "14984", 
  "_rev" : "_USnzqrm---", 
  "_oldRev" : "_USnzqri---" 
}
arangosh> db.example.document(a4);
{ 
  "_key" : "14984", 
  "_id" : "example/14984", 
  "_rev" : "_USnzqrm---", 
  "a" : 1, 
  "b" : 2, 
  "c" : 3, 
  "f" : 6, 
  "e" : 5 
}
arangosh> a5 = db.example.update(a4, {"a" : 1, c : 9, e : 42 });
{ 
  "_id" : "example/14984", 
  "_key" : "14984", 
  "_rev" : "_USnzqrm--_", 
  "_oldRev" : "_USnzqrm---" 
}
arangosh> db.example.document(a5);
{ 
  "_key" : "14984", 
  "_id" : "example/14984", 
  "_rev" : "_USnzqrm--_", 
  "a" : 1, 
  "b" : 2, 
  "c" : 9, 
  "e" : 42, 
  "f" : 6 
}
Use a document handle:
arangosh> a1 = db.example.insert({"a" : 1});
{ 
  "_id" : "example/15036", 
  "_key" : "15036", 
  "_rev" : "_USnzqzG--_" 
}
arangosh> a2 = db.example.update("example/18612115", { "x" : 1, "y" : 2 });
{ 
  "_id" : "example/18612115", 
  "_key" : "18612115", 
  "_rev" : "_USnzqzK---", 
  "_oldRev" : "_USnzqzG---" 
}
Use the keepNull parameter to remove attributes with null values:
arangosh> db.example.insert({"a" : 1});
{ 
  "_id" : "example/15018", 
  "_key" : "15018", 
  "_rev" : "_USnzqwq--_" 
}
arangosh> db.example.update("example/19988371",
........> { "b" : null, "c" : null, "d" : 3 });
{ 
  "_id" : "example/19988371", 
  "_key" : "19988371", 
  "_rev" : "_USnzqwu---", 
  "_oldRev" : "_USnzqwq---" 
}
arangosh> db.example.document("example/19988371");
{ 
  "_key" : "19988371", 
  "_id" : "example/19988371", 
  "_rev" : "_USnzqwu---", 
  "d" : 3, 
  "c" : null, 
  "b" : null 
}
arangosh> db.example.update("example/19988371", { "a" : null }, false, false);
{ 
  "_id" : "example/19988371", 
  "_key" : "19988371", 
  "_rev" : "_USnzqwu--_", 
  "_oldRev" : "_USnzqwu---" 
}
arangosh> db.example.document("example/19988371");
{ 
  "_key" : "19988371", 
  "_id" : "example/19988371", 
  "_rev" : "_USnzqwu--_", 
  "b" : null, 
  "c" : null, 
  "d" : 3 
}
arangosh> db.example.update("example/19988371",
........> { "b" : null, "c": null, "d" : null }, false, false);
{ 
  "_id" : "example/19988371", 
  "_key" : "19988371", 
  "_rev" : "_USnzqwy---", 
  "_oldRev" : "_USnzqwu--_" 
}
arangosh> db.example.document("example/19988371");
{ 
  "_key" : "19988371", 
  "_id" : "example/19988371", 
  "_rev" : "_USnzqwy---" 
}
Patching array values:
arangosh>  db.example.insert({"a" : { "one" : 1, "two" : 2, "three" : 3 },
........> "b" : { }});
{ 
  "_id" : "example/15003", 
  "_key" : "15003", 
  "_rev" : "_USnzquW---" 
}
arangosh> db.example.update("example/20774803", {"a" : { "four" : 4 },
........> "b" : { "b1" : 1 }});
{ 
  "_id" : "example/20774803", 
  "_key" : "20774803", 
  "_rev" : "_USnzquW--_", 
  "_oldRev" : "_USnzquS---" 
}
arangosh> db.example.document("example/20774803");
{ 
  "_key" : "20774803", 
  "_id" : "example/20774803", 
  "_rev" : "_USnzquW--_", 
  "b" : { 
    "b1" : 1 
  }, 
  "a" : { 
    "four" : 4 
  } 
}
arangosh> db.example.update("example/20774803", { "a" : { "one" : null },
........>                                         "b" : null },
........> false, false);
{ 
  "_id" : "example/20774803", 
  "_key" : "20774803", 
  "_rev" : "_USnzqua---", 
  "_oldRev" : "_USnzquW--_" 
}
arangosh> db.example.document("example/20774803");
{ 
  "_key" : "20774803", 
  "_id" : "example/20774803", 
  "_rev" : "_USnzqua---", 
  "a" : { 
    "four" : 4 
  } 
}

Replace By Example

collection.replaceByExample(example, newValue)
example에 해당하는 모든 문서를 newValue로 값을 바꾼다. 그러나 _id, _key, _rev값은 newValue의 값으로 바뀌지 않는다.
collection.replaceByExample(example, newValue, waitForSync)
waitForSync 옵션을 줄 수 있다.
collection.replaceByExample(example, newValue, waitForSync, limit)
replace 되는 document의 개수를 제한할 수 있다. 검색된 document의 개수보다 limit이 적을 경우, 어떤 document가 수정되고, 어떤 document가 안될지는 알 수 없다.
Examples
arangosh> db.example.save({ Hello : "world" });
{ 
  "_id" : "example/6586", 
  "_key" : "6586", 
  "_rev" : "_USnx7om---" 
}
arangosh> db.example.replaceByExample({ Hello: "world" }, {Hello: "mars"}, false, 5);
1

Update By Example

collection.updateByExample(example, newValue)
collection.updateByExample(document, newValue, keepNull, waitForSync)
collection.updateByExample(document, newValue, keepNull, waitForSync, limit)
collection.updateByExample(document, newValue, options)
  • keepNull
  • waitForSync
  • limit
  • mergeObjects
Examples
arangosh> db.example.save({ Hello : "world", foo : "bar" });
{ 
  "_id" : "example/6598", 
  "_key" : "6598", 
  "_rev" : "_USnx7rC---" 
}
arangosh> db.example.updateByExample({ Hello: "world" }, { Hello: "foo", World: "bar" }, false);
1
arangosh> db.example.byExample({ Hello: "foo" }).toArray()
[ 
  { 
    "_key" : "6598", 
    "_id" : "example/6598", 
    "_rev" : "_USnx7rG---", 
    "Hello" : "foo", 
    "foo" : "bar", 
    "World" : "bar" 
  } 
]