ArangoDB - 增删改查操作



在本节中,我们将学习 Arangosh 的不同操作。

以下是 Arangosh 中可能的操作:

  • 创建文档集合
  • 创建文档
  • 读取文档
  • 更新文档

让我们从创建一个新的数据库开始。我们将使用以下代码行创建一个新的数据库:

127.0.0.1:8529@_system> db._createDatabase("song_collection")
true

以下代码行将帮助您切换到新的数据库:

127.0.0.1:8529@_system> db._useDatabase("song_collection")
true

提示符将切换到“@@song_collection”

127.0.0.1:8529@song_collection>

Promt Shift Song Collection

从这里我们将学习 CRUD 操作。让我们在新的数据库中创建一个集合:

127.0.0.1:8529@song_collection> db._createDocumentCollection('songs')

输出

[ArangoCollection 4890, "songs" (type document, status loaded)]
127.0.0.1:8529@song_collection>

让我们向我们的“songs”集合添加一些文档(JSON 对象)。

我们以以下方式添加第一个文档:

127.0.0.1:8529@song_collection> db.songs.save({title: "A Man's Best Friend",
lyricist: "Johnny Mercer", composer: "Johnny Mercer", Year: 1950, _key:
"A_Man"})

输出

{
   "_id" : "songs/A_Man",
   "_key" : "A_Man",
   "_rev" : "_VjVClbW---"
}

让我们向数据库添加其他文档。这将帮助我们学习查询数据的过程。您可以复制这些代码并将其粘贴到 Arangosh 中以模拟该过程:

127.0.0.1:8529@song_collection> db.songs.save(
   {
      title: "Accentchuate The Politics", 
      lyricist: "Johnny Mercer", 
      composer: "Harold Arlen", Year: 1944,
      _key: "Accentchuate_The"
   }
)

{
   "_id" : "songs/Accentchuate_The",
   "_key" : "Accentchuate_The",
   "_rev" : "_VjVDnzO---"
}

127.0.0.1:8529@song_collection> db.songs.save(
   {
      title: "Affable Balding Me", 
      lyricist: "Johnny Mercer", 
      composer: "Robert Emmett Dolan", 
      Year: 1950,
      _key: "Affable_Balding"
   }
)
{
   "_id" : "songs/Affable_Balding",
   "_key" : "Affable_Balding",
   "_rev" : "_VjVEFMm---"
}

如何读取文档

可以使用_key或文档句柄来检索文档。如果不需要遍历集合本身,请使用文档句柄。如果您有集合,则文档函数易于使用:

127.0.0.1:8529@song_collection> db.songs.document("A_Man");
{
   "_key" : "A_Man",
   "_id" : "songs/A_Man",
   "_rev" : "_VjVClbW---",
   "title" : "A Man's Best Friend",
   "lyricist" : "Johnny Mercer",
   "composer" : "Johnny Mercer",
   "Year" : 1950
}

如何更新文档

有两种选项可用于更新保存的数据:replaceupdate

update 函数修补文档,将其与给定的属性合并。另一方面,replace 函数将用新文档替换以前的文档。即使提供了完全不同的属性,替换仍然会发生。我们首先观察一个非破坏性更新,更新歌曲中的“Production”属性:

127.0.0.1:8529@song_collection> db.songs.update("songs/A_Man",{production:
"Top Banana"});

输出

{
   "_id" : "songs/A_Man",
   "_key" : "A_Man",
   "_rev" : "_VjVOcqe---",
   "_oldRev" : "_VjVClbW---"
}

现在让我们读取更新后的歌曲属性:

127.0.0.1:8529@song_collection> db.songs.document('A_Man');

输出

{
   "_key" : "A_Man",
   "_id" : "songs/A_Man",
   "_rev" : "_VjVOcqe---",
   "title" : "A Man's Best Friend",
   "lyricist" : "Johnny Mercer",
   "composer" : "Johnny Mercer",
   "Year" : 1950,
   "production" : "Top Banana"
}

可以使用update函数轻松更新大型文档,尤其是在属性很少的情况下。

相反,replace函数在与同一文档一起使用时将废除您的数据。

127.0.0.1:8529@song_collection> db.songs.replace("songs/A_Man",{production:
"Top Banana"});

现在让我们使用以下代码行检查我们刚刚更新的歌曲:

127.0.0.1:8529@song_collection> db.songs.document('A_Man');

输出

{
   "_key" : "A_Man",
   "_id" : "songs/A_Man",
   "_rev" : "_VjVRhOq---",
   "production" : "Top Banana"
}

现在,您可以观察到文档不再具有原始数据。

如何删除文档

remove 函数与文档句柄结合使用以从集合中删除文档:

127.0.0.1:8529@song_collection> db.songs.remove('A_Man');

现在让我们使用以下代码行检查我们刚刚删除的歌曲属性:

127.0.0.1:8529@song_collection> db.songs.document('A_Man');

我们将得到如下异常错误作为输出:

JavaScript exception in file
'/usr/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 97,7:
ArangoError 1202: document not found
! throw error;
! ^
stacktrace: ArangoError: document not found

at Object.exports.checkRequestResult
(/usr/share/arangodb3/js/client/modules/@arangodb/arangosh.js:95:21)

at ArangoCollection.document
(/usr/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:667:12)
at <shell command>:1:10

Exception Error Output Screen
广告

© . All rights reserved.