ArangoDB自学

官方网站是:https://docs.arangodb.com/3.3/AQL/Tutorial/CRUD.html

1.建立实体,点击Collections ---->add Collection ,填写Name,Type设置为Document

2.在实体中都有唯一的键值"_key",

3."_id" 是一个计算属性,是由实体名称+“/”+_key值组成,这个唯一确定一个实体在一个数据库中

4."_rev"是系统分配的版本号

5."_id","_rev","_key"都是不能改变,都是只能读

  AQL中检索一个信息的语句, 其中_id为“Characters/2861650”

“_key”:为“2861650”,“Characters”是实体名称

RETURN DOCUMENT("Characters", "2861650")
// --- or ---
RETURN DOCUMENT("Characters/2861650")

结果为:

[
{
"_key": "2861650",
"_id": "Characters/2861650",
"_rev": "_V1bzsXa---",
"name": "Ned",
"surname": "Stark",
"alive": true,
"age": 41,
"traits": ["A","H","C","N","P"]
}
]

  查询多个的语句是:

 The DOCUMENT() function also allows to fetch multiple documents at once:

RETURN DOCUMENT("Characters", ["2861650", "2861653"])
// --- or ---
RETURN DOCUMENT(["Characters/2861650", "Characters/2861653"])

结果为:

[
  [
    {
      "_key": "2861650",
      "_id": "Characters/2861650",
      "_rev": "_V1bzsXa---",
      "name": "Ned",
      "surname": "Stark",
      "alive": true,
      "age": 41,
      "traits": ["A","H","C","N","P"]
    },
    {
      "_key": "2861653",
      "_id": "Characters/2861653",
      "_rev": "_V1bzsXa--B",
      "name": "Catelyn",
      "surname": "Stark",
      "alive": false,
      "age": 40,
      "traits": ["D","H","C"]
    }
  ]
]

 

循环读取为:

FOR c IN Characters
    RETURN c

  查询数据的语句:

 https://docs.arangodb.com/3.3/AQL/DataQueries.html 

 过滤查询

FOR doc IN users
    FILTER doc.status == "active"
    SORT doc.name
    LIMIT 10

  循环的插入为:

FOR d IN data
    INSERT d INTO Characters

循环修改为:下面的给Characters表中插入season字段

FOR c IN Characters
    UPDATE c WITH { season: 1 } IN Characters

删除一个实体的语句为:

REMOVE "2861650" IN Characters

循环删除一个实体的语句

FOR c IN Characters
    REMOVE c IN Characters

6.边文档,Edge document ,边文档包含了_from 和 _to,文档类型为edge,

7.图查询,就分为遍历和最短路径

  查询最短路径的语法为:   

FOR vertex[, edge]
  IN OUTBOUND|INBOUND|ANY SHORTEST_PATH
  startVertex TO targetVertex
  GRAPH graphName
  [OPTIONS options]

 注意:其中的startVertex 和 targetVertex 是实体中的_id,如果使用其他的值,会报错。

 具体例子:

FOR v, e IN OUTBOUND SHORTEST_PATH 'circles/A' TO 'circles/D' edges RETURN [v._key, e._key]

 采用Graph graphname的例子

"FOR a IN circles FILTER a._key == 'A' FOR d IN circles FILTER d._key == 'D' FOR v, e IN OUTBOUND SHORTEST_PATH a TO d GRAPH 'traversalGraph' RETURN [v._key, e._key]"

 查询最短路径的语法二:

FOR vertex[, edge]
  IN OUTBOUND|INBOUND|ANY SHORTEST_PATH
  startVertex TO targetVertex
  edgeCollection1, ..., edgeCollectionN
  [OPTIONS options]

  采用边的例子,其中edges,是一个关系表 

"FOR a IN circles FILTER a._key == 'A' FOR d IN circles FILTER d._key == 'D' FOR v, e IN OUTBOUND SHORTEST_PATH a TO d edges RETURN [v._key, e._key]"

使用过滤来提高查询性能。

过滤

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[0].theTruth == true
  RETURN p

取edges中的Truth为 true的

多个过滤条件:

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[0].theTruth == true
     AND p.edges[1].theFalse == false
  FILTER p.vertices[1]._key == "G"
  RETURN p

like ALL edges should have theTruth == true:

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[*].theTruth ALL == true
  RETURN p

Or NONE of the edges should have theTruth == true:

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[*].theTruth NONE == true
  RETURN p

至少有一个满足条件:

It is also possible to define that at least one edge on the path has to fulfill the condition:

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[*].theTruth ANY == true
  RETURN p

 

FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
  FILTER p.edges[*].theTruth ANY == true
  RETURN p

语句:

FOR v IN 1..3 OUTBOUND 'circles/A' GRAPH 'traversalGraph' RETURN v._key

其中1..3中表示遍历度数为1到3,即到A顶点的目标点,可以是度数1,度数2,度数3

同理的写法:

FOR v IN 1..3 OUTBOUND 'circles/A' edges RETURN v._key

 Now we only want the elements of a specific depth (min = max = 2), the ones that are right behind the fork:

最小和最大的深度都为2,结果为:

FOR v IN 2..2 OUTBOUND 'circles/A' GRAPH 'traversalGraph' return v._key

  也可以使用:

FOR v IN 2 OUTBOUND 'circles/A' GRAPH 'traversalGraph' return v._key

参考例子:

https://docs.arangodb.com/3.3/AQL/Graphs/Traversals.html

去掉图中的右边的结点:

FOR v, e, p IN 1..3 OUTBOUND 'circles/A' GRAPH 'traversalGraph' FILTER p.vertices[1]._key != 'G' RETURN v._key

方法二:

FOR v, e, p IN 1..3 OUTBOUND 'circles/A' GRAPH 'traversalGraph' FILTER p.edges[0].label != 'right_foo' RETURN v._key

对比 OUTBOUND / INBOUND / ANY

 

"FOR v IN 1..3 OUTBOUND 'circles/E' GRAPH 'traversalGraph' return v._key");
结果为:
[ 
  "F" 
]
"FOR v IN 1..3 INBOUND 'circles/E' GRAPH 'traversalGraph' return v._key");
结果为:
[ 
  "B", 
  "A" 
]
FOR v IN 1..3 ANY 'circles/E' GRAPH 'traversalGraph' return v._key");
结果为:
[ 
  "F", 
  "B", 
  "C", 
  "D", 
  "A", 
  "G" 
]

注意在:v是目的顶点,不是开始顶点

过滤中的操作符号查看:

https://docs.arangodb.com/3.3/AQL/Operators.html

遍历

按照graphs名称遍历

[WITH vertexCollection1[, vertexCollection2[, ...vertexCollectionN]]]
FOR vertex[, edge[, path]]
  IN [min[..max]]
  OUTBOUND|INBOUND|ANY startVertex
  GRAPH graphName
  [OPTIONS options]

按照边集合遍历

[WITH vertexCollection1[, vertexCollection2[, ...vertexCollectionN]]]
FOR vertex[, edge[, path]]
  IN [min[..max]]
  OUTBOUND|INBOUND|ANY startVertex
  edgeCollection1, ..., edgeCollectionN
  [OPTIONS options]

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值