ArangoDB-积垢作业

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\U集合”

127.0.0.1:8529@song_collection>

Promt Shift Song Collection

从这里我们将研究积垢操作。让我们在新数据库中创建一个集合−

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---"
}

如何阅读文档

The _钥匙 or the document handle can be used to retrieve a document. Use document handle if there is no need to traverse the collection itself. If you have a collection, the document function is easy to use −

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
}

如何更新文档

Two options are available to update the saved data − 代替 and 更新.

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

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"
}

A large document can be easily updated with the 更新 function, especially when the attributes are very few.

In contrast, the 代替 function will abolish your data on using it with the same document.

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

觉得文章有用?

点个广告表达一下你的爱意吧 !😁

评论区

Protected with IP Blacklist CloudIP Blacklist Cloud