操作
打开 Arangosh
arangosh
相关参数:
- --server.database
- --server.endpoint
- --server.username
- --server.password
- --server.authentication : 是否使用认证
arangosh \
--server.endpoint tcp://192.168.173.13:8530 \
--server.username foo \
--server.database test \
--server.authentication true
Database 操作
db._name()
注:在 js 环境下使用 arangodb 包,即 require("@arangodb").db.name()。下同
db._id()
db._path()
db._isSystem()
db._useDatabase(name)
切换数据库
db._databases()
列出当前所有数据库,该操作只能在 _system 数据库下运行
db._createDatabase(name, options, users)
该操作只能在 _system 数据库下运行
options 当前无实际意义,保留字段
users 可选,用于为新数据库创建初始用户。如果指定,则必须是用户对象的列表。其包含:
- username
- passwd
- active boolean,表明用户是否应该处于活动状态,默认值为true
- extra 可选,json object,额外的用户信息
db._dropDatabase(name)
该操作只能在 _system 数据库下运行
db._version()
返回服务器(ArangoDB)版本,注意:不是数据库版本
Collection 操作
ArangoDB 中所有的集合都有唯一的标识符及唯一的名称,其内部使用唯一标识符来查找集合,该标识符由 ArangoDB管理,用户无法控制。为了允许用户使用自己的名称,每个集合拥有用户指定的唯一名称,从用户角度访问集合,应使用集合名称。
db._collection(collection-name [, properties, type])
properties 包含以下属性:
waitForSync
journalSize
isSystem
isVolatile
keyOptions
- type
- allowUserKeys
- increment
- offset
numberOfShards
shardKeys
replicationFactor
type 可选,document 或 edge,默认为 document。推荐使用下面的方法创建 edge 集合
db._createEdgeCollection(collection-name, properties)
db.collection-name
选中一个 collection
db._create(collection-name)
dbdb._collections()
collection.drop(options)
collection.truncate()
清空集合中所有文档
collection.properties()
返回集合的所有属性
collection.figures()
返回集合所有统计信息
collection.load()
加载集合到内存
collection.unload()
unload 操作位于所有查询操作完毕后
collection.revision()
返回集合的 revision id。所谓 revision id 是集合的修订id,当集合内部的文档修改时,revision id 将发生变化。客户端可以依据 revision id 来判断集合是否改变。revision id 是字符串,只可以用来比较是否相等,不能比较大小。
collection.checksum(withRevisions, withData)
计算数据的校验和(checksum)。集群中不适用
collection.rename(new-name)
collection.rotate()
旋转集合的当前日志。集群中不适用
Document 操作
每个document包含三个特殊属性:
- _id
- 集合名称/_key,数据库中的唯一标识,不可变
- _key
- 文档主键,当前collection中的唯一标识,不可变
- _rev
- revision id,ArangoDB进行维护
Collection
collection.all()
获取集合所有文档,返回 cursor
collection.all().skip(1).limit(3).toArray()
collection.save(document)
collection.byExample(example)
类似 mongo的 find,返回 cursor
collection.byExample(path1, value1, ...)
collection.firstExample(example)
类似 mongo 的 findOne
collection.firstExample(path1, value1, ...)
collection.any()
返回一个随机的 document
collection.count()
collection.toArray()
不要在生产环境使用
collection.document(selector)
selector 必须包含 _id 或 _key。下同
返回文档。优先使用id,其次使用key,如果id/key找不到,报错。如果指定了 _rev,并 __rev 不同,报错。
如果在 arangod服务器上执行(例如:Foxx) ,出于性能原因将返回不可变的文档对象,要更新或修补返回的文档,需要先将其克隆/复制到一个常规的JavaScript对象中。如果从 arangosh或其他任何客户端调用,则不需要这样做。
collection.document(document-handle)
collection.document(document-key)
collection.document(array)
collection.document(object)
collection.exists(selector)
collection.exists(document-handle)
collection.exists(document-key)
collection.exists(array)
collection.exists(object)
collection.documents(keys)
collection.insert(data [, options])
插入文档,返回生成新文档。
_key 如指定,必须唯一。id与rev不可指定。
collection.insert(array)
collection.insert(array, options)
上面两个 insert 的变体,返回结果也是 array ,执行过程中如果遇到错误,将直接停止。
collection.replace(selector, data [, options])
返回具有属性_id,_key,_rev和_oldRev的文档。
collection.replace(document-handle, data)
collection.replace(document-handle, data, options)
collection.replace(document-key, data)
collection.replace(document-key, data, options)
collection.replace(selectorarray, dataarray)
collection.replace(selectorarray, dataarray, options)
collection.update(selector, data [, options])
返回具有属性_id,_key,_rev和_oldRev的文档。
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)
collection.remove(selector [, options])
返回具有属性_id,_key,_rev和_Rev的文档。
collection.remove(document-handle)
collection.remove(document-handle, options)
collection.remove(document-key)
collection.remove(document-handle, options)
collection.remove(selectorarray)
collection.remove(selectorarray,options)
collection.removeByKeys(keys)
collection.removeByExample(example)
collection.removeByExample(example [, waitForSync, limit])
collection.replaceByExample(example, newValue [, waitForSync, limit])
collection.updateByExample(example, newValue [, keepNull, waitForSync, limit])
collection.updateByExample(document, newValue, options)
options 包括:
- keepNull
- waitForSync
- limit
- mergeObjects
collection.type()
返回 collection 的类型:
- 2:document collection
- 3:edge collection
Database
db._document(object)
注:object 中必须包含 _id,下同。推荐直接使用下面这个:
db.document(document-handle)
注:document-handle即documen._id
db._exists(object)
db._exists(document-handle)
db._replace(object, data [, options])
db._replace(document-handle, data [, options])
db._replace(object, data [, options])
db._replace(document-handle, data [, options])
db._update(object, data [, options])
db._update(document-handle, data [, options])
db._remove(object [, options])
db._remove(document-handle [, options])
Edge 操作
edge-collection.edges(vertex)
获取跟 vertex 相关的所有edge。vertex可以是document,也可以是document 的 _id。
edge-collection.edges(vertices)
同上,结果的 edges 自动去重。
edge-collection.inEdges(vertex)
获取以 vertex 为结尾的所有 edges
edge-collection.inEdges(vertices)
同上。
edge-collection.outEdges(vertex)
获取以 vertex 为起始的所有 edges
edge-collection.outEdges(vertices)