开发手册 欢迎您!
软件开发者资料库

ArangoDB - Crud Operations

ArangoDB Crud操作 - 从简单和简单的步骤学习ArangoDB,从基本到高级概念,包括多模型第一数据库,优势,基本概念和术语,系统要求,命令行,Web界面,示例案例场景,数据模型和建模,数据库方法,Crud操作,使用Web界面的Crud操作,使用AQL查询数据,AQL示例查询,如何部署。

在本章中,我们将学习与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>

让我们在"歌曲"系列中添加一些文档(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}

如何更新文档

有两个选项可用于更新已保存的数据 :  替换更新.

更新功能会修补文档,将其与给定的属性合并.另一方面,替换功能将用新的替换先前的文档.即使提供完全不同的属性,仍将进行替换.我们将首先观察非破坏性更新,更新歌曲中的属性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"}

使用更新功能可以轻松更新大型文档,尤其是当属性非常少时.

相比之下,替换函数将取消使用相同文档的数据.

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 foundat 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 :1:10


异常错误输出屏幕