學習主要內容
(1)數據模型
(2)相關的專業術語
(3)建造數據模型時的考慮,以使得在自然數據結構和性能之間平衡。
(4)使用arangosh來進行數據庫的操作,權限設定,增加,刪除等設置。
(5)如何操作collections和document,增刪改查。使用web界面Aardvark來進行操作。
1、相關概念
(1)數據庫交互
arangodb是一個服務器數據庫,他是用tcp連接,http協議來以json形式傳輸document。數據庫提供了一個REST API來與數據庫交互。
有一個web界面可以連接數據庫,叫做Aardvark;有一個shell可以連接數據庫叫做arangosh。另外還有很多的驅動程序(drivers)供不同大的語言來使用arangodb.所有的這些連接都有http接口,以使得不用再去實現底層的鏈接功能。
(2)數據模型
存儲在arangodb中的文件嚴格遵循json格式,雖然實際是以二進制存儲的(VelocyPack)
document:可以有0個或者多個屬性,每個屬性都有一個屬性值。值可以是原子類型,即數字,字符串,布爾值或空值,也可以是復合類型,即數組或另一個document對象。我們可以任意的嵌套數據結構以代表實際情況。
collections:是有多個documents組成的。一個collection包含0個或多個document.colletions相當於數據庫的表,document相當於一條條的記錄。只是collections沒有自己的列標題,每個documetn可以有不同的結構,都以鍵值對的形式來包含所有信息。collection並不要求所有的document都有相同的結構。
有兩種collections:document collection(在graph中也被稱為vertex collections)和edge collection。edge collection也是存儲document的,只不過document中有兩個特殊的屬性,分別是_from和_to
可以用來在documetn中創建連接。通常情況下,在document colleciton當中的兩個document會被edge collection中的一個document連接起來。這就是arangodb graph的數據模型。這個數據模型也遵循了一般的圖數據模型規則,有方向_from _to,有label來指明關系的性質,只是除此之外,edge在arangodb中還是一個完整的document,可以有document的所有屬性定義。
databases:collections必須在數據庫中。可以有多個數據庫,不同數據庫之間的數據是完全隔離的。有一個特殊的數據庫_system,這個數據庫不可以刪除,它可以用來管理數據庫用戶。
(3)數據提取
Queries:用來增刪改查documetns,查詢語言使用AQL
cursors:可以用來迭代查詢結果,不至於使得太大的查詢結果一下加載到內存。
Indexes :可以用來加速查詢。有很多種類型的索引,如hash index,geo index
2、數據庫操作
數據庫操作都是在arangosh中進行
連接數據庫:
arangosh --server.database test
注意,在arangodb中僅支持連接一個數據庫。因此在命令、腳本中所有的操作要針對同一個數據庫。
我們可以使用js來操作數據庫。下面的操作有些只能在_system數據庫中進行。
獲取數據庫名稱:db._name
require("@arangodb").db._name()
獲取數據庫id
require("@arangodb").db._id()
獲取數據庫路徑
require("@arangodb").db._path()
檢查數據庫是否為_system數據庫
db._isSystem()
切換使用數據庫
db._useDatabase(name)
當然當前用戶要有操作該數據庫的權限,且該數據庫存在。
列出所有的數據庫
db._databases()
創建數據庫
db._createDatabase(name, options, users)
注意,創建數據庫並沒有改變數據庫,如果要改變使用過的數據庫還是要使用db._useDatabase。現在options還沒有作用,因此寫為空{}就行了。name指定數據庫名稱,users指定可以操作該數據庫的用戶信息。如果不聲明users,默認就是root用戶,密碼為空。
users包含下面一些信息:
- username:用戶名作為字符串。該屬性是必需的。
- passwd:用戶密碼為字符串。如果未指定,則默認為空字符串。
- active:一個布爾標志,指示用戶帳戶是否應該處於活動狀態。默認值為true。
- extra:帶有額外用戶信息的可選JSON對象。extra中包含的數據將為用戶存儲,但ArangoDB不會進一步解釋。
用戶操作
require("@arangodb/users").save(username, password, true); require("@arangodb/users").update(username, password, true); require("@arangodb/users").remove(username);
示例,創建數據庫
db._createDatabase("newDB", {}, [{ username: "newUser", passwd: "123456", active: true}])
刪除數據庫
db._dropDatabase(name)
注意刪除數據庫盡可以在_system數據庫中進行操作。
數據庫引擎運行信息
db._engineStats()
使用數據庫注意事項:
復制數據庫配置在每一個數據庫層面,用來記錄復制的日志信息。
Foxx應用必須聲明一個對應的數據庫。
3、collections
在arangodb中每一個collection都必須有唯一的名字和標識符。
連接到collections
db._collection(collection-name)
查看collection的名字
db.collection-name
這將要返回一個名字為collection_name的collection,如果沒有的話講創建一個collection_name的collection,這種創建行為是不被推薦的。
創建collection
db._create(collection-name)
同步復制
從arangodb3.0開始,增加了一個在arangodb 集群中自動復制所有數據的功能。每一個collection可以在創建的時候配置‘復制因子’以便能夠實現共享。復制因子k的意思是在集群中的k的服務器上保持了k個副本,每個colloction操作自動添加到每個副本上,保持同步。
這是使用leader/followers模型組織的。在所有的時刻,所有的服務器總有一個是leader(正在持有“replicas”),其他的都是followers,這個配置是被保存在Agence當中。
每個寫操作都會被代理自動發送到leader,然后所有的followers都會自動復制該操作,直到所有的復制成功后才會返回寫成功狀態。leader服務器保存了一個記錄,那些followers現在是同步的。如果一個follower由於網絡狀態掉線了,那么leader將會暫時的刪除該follower的狀態,以便可以恢復服務。當網路恢復后,follower將會自動連接leader並更新到最新狀態。
如果這個leader掛了,那么Agence將會在大約15秒內啟動失敗處理方案,將其中一個follower提升為leader.其他的followers將會自動和新的leader保持同步,如果原先的leader重新連接了,那么他將會被作為follower自動連接到新leader.通常情況下這個處理方案是協調器自動解決的,用戶不會看到任何的錯誤代碼。
顯然,這種容錯是以增加延遲為代價的。每個寫入操作都需要額外的網絡往返來進行跟隨者的同步復制,但是對所有關注者的所有復制操作都會同時發生。這就是默認復制因子為1的原因,這意味着沒有復制
有關如何為集合打開同步復制的詳細信息,請參閱“數據庫方法db._create(collection-name)
”一節中的 數據庫方法。
(1)collection method
刪除操作:drop
collection.drop(options)
刪除一個collection內的所有index和data.
如果想要刪除一個_system內的collection,需要在options中指定isSystem 為true
在集群中刪除一個被其他colletion共享的collection是被禁止的,除非先刪除所有的依賴collection
示例:
arangosh> col = db.example; [ArangoCollection 15512, "example" (type document, status loaded)] arangosh> col.drop(); arangosh> col; [ArangoCollection 15512, "example" (type document, status deleted)] arangosh> col = db._example; [ArangoCollection 15516, "_example" (type document, status loaded)] arangosh> col.drop({ isSystem: true }); arangosh> col; [ArangoCollection 15516, "_example" (type document, status deleted)]
截斷操作:truncate
刪除collection中的所有documetns但是保持所有的indexes
arangosh> col = db.example; [ArangoCollection 15674, "example" (type document, status loaded)] arangosh> col.save({ "Hello" : "World" }); { "_id" : "example/15678", "_key" : "15678", "_rev" : "_XaSNdV---_" } arangosh> col.count(); 1 arangosh> col.truncate(); arangosh> col.count(); 0
屬性方法:properties
獲取或設置collection的properties屬性。
返回包含所有集合屬性的對象。collection.properties()
-
waitForSync:如果為true,則只有在將數據同步到磁盤后才會返回創建文檔。
-
journalSize:日志的大小(以字節為單位)。此選項僅對MMFiles存儲引擎有意義。
-
isVolatile:如果為true,則集合數據將僅保留在內存中,ArangoDB不會將數據寫入或同步到磁盤。此選項僅對MMFiles存儲引擎有意義。
-
keyOptions(可選)用於密鑰生成的其他選項。這是一個包含以下屬性的JSON數組(注意:某些屬性是可選的):
- type:用於集合的密鑰生成器的類型。
- allowUserKeys:如果設置為true,則允許在文檔的_key屬性中提供自己的鍵值。如果設置為false,則密鑰生成器將僅負責生成密鑰,並且在文檔的_key屬性中提供自己的密鑰值被視為錯誤。
- increment:自動增量密鑰生成器的增量值。不用於其他密鑰生成器類型。
- offset:自動增量密鑰生成器的初始偏移值。不用於其他密鑰生成器類型。
-
indexBuckets:分割使用哈希表的索引的桶數。默認值為16,此數字必須是2的冪且小於或等於1024.此選項僅對MMFiles存儲引擎有意義。
對於非常大的集合,應該增加這一點以避免在最初構建或調整哈希表時長時間暫停,因為存儲桶單獨調整大小並且最初可以並行構建。例如,64可能是具有100 000 000個文檔的集合的合理值。目前,只有邊緣索引尊重此值,但在將來的ArangoDB版本中可能會遵循其他索引類型。下次加載集合時將應用更改(見下文)。
在群集設置中,結果還將包含以下屬性:
-
numberOfShards:集合的分片數。
-
shardKeys:包含用於確定文檔的目標分片的文檔屬性的名稱。
-
replicationFactor:確定每個分片在不同DBServer上保留的副本數。
collection.properties(properties)
更改集合屬性。屬性必須是具有以下一個或多個屬性的對象:
-
waitForSync:如果為true,則只有在將數據同步到磁盤后才會返回創建文檔。
-
journalSize:日志的大小(以字節為單位)。此選項僅對MMFiles存儲引擎有意義。
-
indexBuckets:如上所述,更改僅在下次加載集合時應用。此選項僅對MMFiles存儲引擎有意義。
-
replicationFactor:更改保留在不同DBServer上的分片副本數,有效值為1-10范圍內的整數(僅限集群)
注意:在創建日志或數據文件后,無法更改日志大小。更改此參數只會影響新創建的日記帳。另請注意,您不能將日志大小降低到小於已存儲在集合中的最大文檔的大小。
注意:創建集合后,某些其他集合屬性(如type,isVolatile或keyOptions)無法更改。
示例
arangosh> db.example.properties(); { “doCompact”:true, “journalSize”:33554432, “isSystem”:false, “isVolatile”:false, “waitForSync”:false, “keyOptions”:{ “type”:“traditional”, “allowUserKeys”:true, “ lastValue“:0 }, “indexBuckets”:8 } arangosh> db.example.properties({ waitForSync:true }); { “doCompact”:true, “journalSize”:33554432, “isSystem”:false, “isVolatile”:false, “waitForSync”:true, “keyOptions”:{ “type”:“traditional”, “allowUserKeys”:true, “ lastValue“:0 }, “indexBuckets”:8 }
Figures
返回collection的統計信息
collection.figures(),注意顯示統計信息,將會把collection加載到內存中。
- alive.count:集合的所有數據文件和日志中當前活動文檔的數量。此圖中未報告僅包含在預寫日志中的文檔。
- alive.size:集合的所有活動文檔使用的總大小(以字節為單位)。此圖中未報告僅包含在預寫日志中的文檔。
- dead.count:死文件的數量。這包括已刪除或替換為較新版本的文檔版本。僅在預讀日志中包含的已刪除或替換的文檔不會在此圖中報告。
- dead.size:所有死文檔使用的總大小(以字節為單位)。
- dead.deletion:刪除標記的總數。僅包含在預寫日志中的刪除標記未在此圖中報告。
- datafiles.count:數據文件的數量。
- datafiles.fileSize:數據文件的總文件大小(以字節為單位)。
- journals.count:日志文件的數量。
- journals.fileSize:日志文件的總文件大小(以字節為單位)。
- compactors.count:壓縮文件的數量。
- compactors.fileSize:壓縮文件的總文件大小(以字節為單位)。
- shapefiles.count:形狀文件的數量。僅出於兼容性原因,不推薦使用此值。自ArangoDB 2.0及更高版本以來,該值始終為0。
- shapefiles.fileSize:形狀文件的總文件大小。僅出於兼容性原因,不推薦使用此值。ArangoDB 2.0及更高版本中的值始終為0。
- shapes.count:集合中使用的形狀總數。這包括不再使用的形狀。此圖中不報告預寫日志中包含的形狀。
- shapes.size:所有形狀的總大小(以字節為單位)。這包括不再使用的形狀。此圖中不報告預寫日志中包含的形狀。
- attributes.count:集合中使用的屬性總數。注意:該值包括不再使用的屬性數據。此圖中未報告僅包含在預寫日志中的屬性。
- attributes.size:屬性數據的總大小(以字節為單位)。注意:該值包括不再使用的屬性數據。此圖中未報告僅包含在預寫日志中的屬性。
- indexes.count:為集合定義的索引總數,包括預定義的索引(例如主索引)。
- indexes.size:為索引分配的總內存(以字節為單位)。
- lastTick:存儲在集合日志中的最后一個標記的刻度。如果集合還沒有日記,則可能為0。
- uncollectedLogfileEntries:此集合的預寫日志中尚未傳輸到日記帳或數據文件的標記數。
- documentReferences:JavaScript代碼當前包含的數據文件中對文檔的引用數。此信息可用於調試壓縮和卸載問題。
- waitingFor:一個可選的字符串值,包含有關哪個對象類型位於集合清理隊列頭部的信息。此信息可用於調試壓縮和卸載問題。
- compactionStatus.time:上次執行集合壓縮的時間點。此信息可用於調試壓縮問題。
- compactionStatus.message:上次為集合運行壓縮時執行的操作。此信息可用於調試壓縮問題。
注意:僅在結果中不報告存儲在預寫日志中的收集數據。收集預寫日志時,可能會將文檔添加到集合的日志和數據文件中,這可能會修改集合的數字。還要注意的是waitingFor
,並compactionStatus
在集群中的協調調用時可能是空的。
此外,不報告集合和索引參數JSON文件的文件大小。這些文件通常應該具有每個字節的大小。另請注意,fileSize值以字節為單位報告,並反映邏輯文件大小。某些文件系統可能使用優化(例如稀疏文件),因此實際的物理文件大小有些不同。目錄和子目錄可能還需要文件系統中的空間,但fileSize結果中不會報告此空間。
這意味着所報告的數字並不能真實地反映集合的實際磁盤使用情況。集合的實際磁盤使用量通常略高於報告的fileSize值的總和 。仍然可以將fileSize值的總和用作磁盤使用的下限近似值。
示例
arangosh> db.demo.figures() { "indexes" : { "count" : 1, "size" : 32128 }, "documentReferences" : 0, "waitingFor" : "-", "alive" : { "count" : 1, "size" : 184 }, "dead" : { "count" : 0, "size" : 0, "deletion" : 0 }, "compactionStatus" : { "message" : "skipped compaction because collection has no datafiles", "time" : "2018-09-10T19:57:24Z", "count" : 0, "filesCombined" : 0, "bytesRead" : 0, "bytesWritten" : 0 }, "datafiles" : { "count" : 0, "fileSize" : 0 }, "journals" : { "count" : 1, "fileSize" : 33554432 }, "compactors" : { "count" : 0, "fileSize" : 0 }, "revisions" : { "count" : 1, "size" : 48192 }, "lastTick" : 97, "uncollectedLogfileEntries" : 0 }
Load
collection.load()將collection加載到內存中。
集群中的collection是始終加載的。
Revision
返回collection發生修改后的id collection.revision()
Path
返回集合的物理路徑 collection.path()
在集群中,這個是沒有意義的。
Checksum
collection.checksum(withRevisions, withData)
這個checksum會為collection中的所有documents 的keys計算一個hash碼。
如果可選參數withRevisions設置為true,則文檔的修訂ID也包含在哈希計算中。
如果可選參數withData設置為true,則所有用戶定義的文檔屬性也會進行校驗和。在校驗和中包括文檔數據將使計算更慢,但更准確。
得出的hash嗎可以校驗兩個collection是否完全一致。
此方法在集群中不可用
Unload
collection.unload()從內存中卸載collection
在集群中無法卸載
示例
arangosh> col = db.example; [ArangoCollection 7427,“示例”(類型文檔,已加載狀態)] arangosh> col.unload(); arangosh> col; [ArangoCollection 7427,“示例”(類型文檔,狀態卸載)]
Rename
重命名集合 collection.rename(new-name)
使用新名稱重命名集合。該新名稱不能已被用於不同的集合。新名稱也必須是有效的集合名稱。有關有效集合名稱的更多信息,請參閱命名約定。
如果由於任何原因重命名失敗,則會引發錯誤。如果重命名集合成功,則還會_graphs
在當前數據庫中集合內的所有圖形定義中重命名集合。
注意:此方法在群集中不可用。
arangosh> c = db.example; [ArangoCollection 15668, "example" (type document, status loaded)] arangosh> c.rename("better-example"); arangosh> c; [ArangoCollection 15668, "better-example" (type document, status loaded)]
Rotate
collection.rotate()使集合的當前日志成為只讀數據文件。此方法特定於MMFiles存儲引擎,並且在群集中不可用
(2)Database Methods
獲取collection
db._collection(collection-name)
示例
arangosh> db._collection("demo"); [ArangoCollection 92, "demo" (type document, status loaded)]
create
db._create(collection-name, properties) db._create(collection-name, properties, type) db._createEdgeCollection db._createDocumentCollection
All Collections
返回所有集合 db._collections()
Collection Name
db.collection-name
4、documents
每個document有唯一的主鍵在collection中,另外document通過加上collection前綴生成文檔句柄(document handle ),在所有的collections中也是全局唯一的。文檔的每次修改,都可以通過document revision。任何事務只能看到文檔的單個修訂。例如:
{ "_id" : "myusers/3456789", "_key" : "3456789", "_rev" : "14253647", "firstName" : "John", "lastName" : "Doe", "address" : { "street" : "Road To Nowhere 1", "city" : "Gotham" }, "hobbies" : [ {name: "swimming", howFavorite: 10}, {name: "biking", howFavorite: 6}, {name: "programming", howFavorite: 4} ] }
_id是文檔句柄:document handle ,由集合名/_key組成,在整個數據庫中是惟一的。
_key:是primary key,在collection中是惟一的,可以自定義。
_rev:是 document revision 。由於arangodb支持mvvc(多版本並發控制),document可以有多個版本。他是唯一的在document文檔中。在3.1版本后它實際是一個時間搓。在一個分片中,保證兩個不同的文檔修訂版具有不同的_rev字符串,即使它們以相同的毫秒寫入,並且這些標記是升序的。但請注意,群集中的不同服務器可能具有時鍾偏差,因此在不同的分片之間甚至在不同的集合之間,時間戳不能保證具有可比性。混合邏輯時鍾功能可以解決此問題:無論何時從群集中的某個服務器A向另一個B發送消息,都可以確保在消息到達后在B上獲取的任何時間戳大於任何時間戳。 文檔修訂可用於有條件地查詢,更新,替換或刪除數據庫中的文檔。要查找文檔的特定修訂版,您需要document handle或key以及document revesion。
創建文檔時,用戶可以指定屬性的值。一旦創建了文檔_id
,_key
值就不可變。該_rev
值由ArangoDB自動維護。
Multiple Documents in a single Command
一條命令操作多個documents。解決方法使用該數組來代替單個document。多文檔操作僅限於單個文檔或邊緣collection.
collection.all()
返回的結果是一個可迭代的所有的documents.您可以使用 toArray,next或hasNext來訪問結果。可以使用skip和limit運算符限制結果
示例
arangosh> db.five.save({ name:“one” }); arangosh> db.five.save({ name:“two” }); arangosh> db.five.save({ name:“three” }); arangosh> db.five.save({ name:“four” }); arangosh> db.five.save({ name:“five” }); arangosh> db.five.all().limit(2).toArray();
Query by example
collection.byExample(example)
根據示例返回一個documents的迭代器。
示例:
arangosh> db.users.save({ name: "Gerhard" }); { "_id" : "users/166", "_key" : "166", "_rev" : "_XaSNNa---_" } arangosh> db.users.save({ name: "Helmut" }); { "_id" : "users/170", "_key" : "170", "_rev" : "_XaSNNa---B" } arangosh> db.users.save({ name: "Angela" }); { "_id" : "users/173", "_key" : "173", "_rev" : "_XaSNNa---D" } arangosh> var a = db.users.byExample( {"name" : "Angela" } ); arangosh> while (a.hasNext()) print(a.next()); { "_key" : "173", "_id" : "users/173", "_rev" : "_XaSNNa---D", "name" : "Angela" }
First Example
返回符合指定示例的第一個document
arangosh> db.users.firstExample("name", "Angela"); { "_key" : "15547", "_id" : "users/15547", "_rev" : "_XaSNdGa--D", "name" : "Angela" }
Any
collection.any()返回隨機的一個document
Count
返回集合中的活動文檔數
toArray
collection.toArray()將集合轉換為文檔數組。切勿在生產環境中使用此調用,因為它基本上會在RAM中創建集合的副本,該副本將根據集合中文檔的數量和大小使用資源。
Document
collection.document(object)返回一個document通過id或key
如果從服務器上運行改命令得到的document將是一個不可變對象。如果需要修改,則需要先將結果復制為一個js對象 。如果從arangosh或客戶端運行,可以不必理會。
collection.document(document-handle) collection.document(document-key) collection.document(array)
示例
id:
arangosh> db.example.document("example/2873916"); { "_key" : "2873916", "_id" : "example/2873916", "_rev" : "_XaSNdqi--_" }
或者
arangosh> db.example.document({_id: "example/2873916"}); { "_key" : "2873916", "_id" : "example/2873916", "_rev" : "_XaSNdoe--_" }
key:
arangosh> db.example.document("2873916"); { "_key" : "2873916", "_id" : "example/2873916", "_rev" : "_XaSNdna--_" }
使用數組
arangosh> db.example.document(["2873916","2873917"]); [ { "_key" : "2873916", "_id" : "example/2873916", "_rev" : "_XaSNdpi--_" }, { "_key" : "2873917", "_id" : "example/2873917", "_rev" : "_XaSNdpi--B" } ]
Exists
collection.exists(object)檢查一個document是否存在。參數同document.返回結果為id,key或者null
使用keys查找
collection.documents(keys)
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.documents(keys);
Insert
collection.insert(data)
根據給定的data在collection中創建新的document.如果不指定key,那么將自動生成key,和rev.如果指定key,文件中必須不能由key .
該方法將要返回一個docuemnt
collection.insert(data, options)
options必須是對象
- waitForSync:即使在整個集合中禁用了waitForSync標志的情況下,也可以強制將文檔創建操作同步到磁盤。因此,waitForSync選項可用於強制僅特定操作的同步。要使用它,請將waitForSync參數設置 為true。如果未指定waitForSync參數或將其設置為false,則應用集合的默認 waitForSync行為。該waitForSync參數不能用於關閉有一個默認集合同步waitForSync的價值真。
- silent:如果此標志設置為true,則該方法不返回任何輸出。
- returnNew:如果這個標志被設置為真實,完整的新文檔的屬性下的輸出返回新。
collection.insert(array)
collection.insert(array, options)
這兩個變體允許對整個參數數組執行操作。行為就像是分別在數組的所有成員上調用insert,並且所有結果都在數組中返回
示例
arangosh> db.example.insert({ Hello : "World" }); { "_id" : "example/15885", "_key" : "15885", "_rev" : "_XaSNdjG--_" } arangosh> db.example.insert({ Hello : "World" }, {waitForSync: true}); { "_id" : "example/15889", "_key" : "15889", "_rev" : "_XaSNdjG--B" }
arangosh> db.example.insert([{ Hello:“World” },{ Hello:“there” }]) [ { “_id”:“example / 15865”, “_ key”:“15865”, “_ rev”:“_ XaSNdhi --_” }, { “_id”:“example / 15869”, “_ key”:“15869”, “_ rev”:“_ XaSNdhi - B” } ] arangosh> db.example.insert([{ Hello:“World” },{}],{ waitForSync:true }); [ { “_id”:“example / 15873”, “_ key”:“15873”, “_ rev”:“_ XaSNdhi - D” }, { “_id”:“example / 15877”, “_ key”:“15877”, “_ rev”:“_ XaSNdhm --_” } ]
Replace
collection.replace(selector, data)
selector可以是key或者id,或者數組,data是要修改的數據
返回一個document
collection.replace(selector, data, options)
waitForSync:即使在整個集合中禁用了waitForSync標志的情況下,也可以強制將文檔創建操作同步到磁盤。因此,waitForSync選項可用於強制僅特定操作的同步。要使用它,請將waitForSync參數設置 為true。如果未指定waitForSync參數或將其設置為false,則應用集合的默認 waitForSync行為。該waitForSync參數不能用於關閉有一個默認集合同步waitForSync的價值真。
overwrite:如果此標志設置為true,則忽略選擇器中的_rev屬性。
returnNew:如果這個標志被設置為真實,完整的新文檔的屬性下的輸出返回新。
returnOld:如果此標志設置為true,則在屬性old下的輸出中返回文檔的完整先前版本。
silent:如果此標志設置為true,則不返回任何輸出。
arangosh> a1 = db.example.insert({ a : 1 }); { "_id" : "example/15996", "_key" : "15996", "_rev" : "_XaSNdvu--_" } arangosh> a2 = db.example.replace(a1, { a : 2 }); { "_id" : "example/15996", "_key" : "15996", "_rev" : "_XaSNdvy--_", "_oldRev" : "_XaSNdvu--_" } arangosh> a3 = db.example.replace(a1, { a : 3 }); [ArangoError 1200: precondition failed]
使用id
arangosh> a1 = db.example.insert({ a : 1 }); { "_id" : "example/3903045", "_key" : "3903045", "_rev" : "_XaSNdw6--B" } arangosh> a2 = db.example.replace("example/3903044", { a : 2 }); { "_id" : "example/3903044", "_key" : "3903044", "_rev" : "_XaSNdw6--D", "_oldRev" : "_XaSNdw6--_" }
Update
collection.update(selector, data)
collection.update(selector, data, options)
示例
arangosh> a1 = db.example.insert({"a" : 1}); arangosh> a2 = db.example.update(a1, {"b" : 2, "c" : 3}); arangosh> a3 = db.example.update(a1, {"d" : 4}); arangosh> a4 = db.example.update(a2, {"e" : 5, "f" : 6 }); arangosh> db.example.document(a4); arangosh> a5 = db.example.update(a4, {"a" : 1, c : 9, e : 42 }); arangosh> db.example.document(a5);
Remove
collection.remove(selector)
刪除一個document,返回_id, _key and _rev
collection.remove(selector, options)
- waitForSync:即使在整個集合中禁用了waitForSync標志的情況下,也可以強制將文檔創建操作同步到磁盤。因此,waitForSync選項可用於強制僅特定操作的同步。要使用它,請將waitForSync參數設置 為true。如果未指定waitForSync參數或將其設置為false,則應用集合的默認 waitForSync行為。該waitForSync參數不能用於關閉有一個默認集合同步waitForSync的價值真。
- overwrite:如果此標志設置為true,則忽略選擇器中的_rev屬性。
- returnOld:如果此標志設置為true,則在屬性old下的輸出中返回文檔的完整先前版本。
- silent:如果此標志設置為true,則不返回任何輸出。
collection.removeByKeys(keys)
按key刪除
該方法將返回一個對象,該對象包含已刪除的子屬性中已刪除文檔的數量,以及被忽略的子屬性中未刪除/忽略的文檔的數量。
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)
collection.removeByExample(document, waitForSync)
collection.removeByExample(document, waitForSync, limit)
arangosh> db.example.removeByExample({ Hello:“world” });
Replace By Example
arangosh> db.example.save({ Hello:“world” }); { “_id”:“example / 6595”, “_ key”:“6595”, “_ rev”:“_ XaSNN7u --_” } arangosh> db.example.replaceByExample({ Hello:“world” },{ Hello:“mars” },false,5); 1
Update By Example
collection.updateByExample(example, newValue)
arangosh> db.example.save({ Hello : "world", foo : "bar" }); { "_id" : "example/6608", "_key" : "6608", "_rev" : "_XaSNN9K--_" } arangosh> db.example.updateByExample({ Hello: "world" }, { Hello: "foo", World: "bar" }, false); 1 arangosh> db.example.byExample({ Hello: "foo" }).toArray() [ { "_key" : "6608", "_id" : "example/6608", "_rev" : "_XaSNN9O--_", "Hello" : "foo", "foo" : "bar", "World" : "bar" } ]
Collection type
collection.type()
Returns the type of a collection. Possible values are:
- 2: document collection
- 3: edge collection
Get the Version of ArangoDB
db._version()
Edges
arangosh> db._create("vertex"); [ArangoCollection 7451, "vertex" (type document, status loaded)] arangosh> db._createEdgeCollection("relation"); [ArangoCollection 7454, "relation" (type edge, status loaded)] arangosh> var myGraph = {}; arangosh> myGraph.v1 = db.vertex.insert({ name : "vertex 1" }); { "_id" : "vertex/7458", "_key" : "7458", "_rev" : "_XaSNOXe--_" } arangosh> myGraph.v2 = db.vertex.insert({ name : "vertex 2" }); { "_id" : "vertex/7462", "_key" : "7462", "_rev" : "_XaSNOXe--B" } arangosh> myGraph.e1 = db.relation.insert(myGraph.v1, myGraph.v2, ........> { label : "knows"}); { "_id" : "relation/7465", "_key" : "7465", "_rev" : "_XaSNOXe--D" } arangosh> db._document(myGraph.e1); { "_key" : "7465", "_id" : "relation/7465", "_from" : "vertex/7458", "_to" : "vertex/7462", "_rev" : "_XaSNOXe--D", "label" : "knows" } arangosh> db.relation.edges(myGraph.e1._id); [ ]
from不包含在內,to包含在內,因此查找from為空
edge-collection.inEdges(vertex)
可以檢查是否在edges中
arangosh> db._create("vertex"); [ArangoCollection 7472, "vertex" (type document, status loaded)] arangosh> db._createEdgeCollection("relation"); [ArangoCollection 7475, "relation" (type edge, status loaded)] arangosh> myGraph.v1 = db.vertex.insert({ name : "vertex 1" }); { "_id" : "vertex/7479", "_key" : "7479", "_rev" : "_XaSNOaC--_" } arangosh> myGraph.v2 = db.vertex.insert({ name : "vertex 2" }); { "_id" : "vertex/7483", "_key" : "7483", "_rev" : "_XaSNOaC--B" } arangosh> myGraph.e1 = db.relation.insert(myGraph.v1, myGraph.v2, ........> { label : "knows"}); { "_id" : "relation/7486", "_key" : "7486", "_rev" : "_XaSNOaG--_" } arangosh> db._document(myGraph.e1); { "_key" : "7486", "_id" : "relation/7486", "_from" : "vertex/7479", "_to" : "vertex/7483", "_rev" : "_XaSNOaG--_", "label" : "knows" } arangosh> db.relation.inEdges(myGraph.v1._id); [ ] arangosh> db.relation.inEdges(myGraph.v2._id); [ { "_key" : "7486", "_id" : "relation/7486", "_from" : "vertex/7479", "_to" : "vertex/7483", "_rev" : "_XaSNOaG--_", "label" : "knows" } ]
edge-collection.outEdges(vertex)
和inEdges正好相反
Database Methods
db._document(object)
db._exists(object)
db._replace(selector, data)
db._update(selector, data)
db._remove(selector)