ArangoDB - Crud 操作
在本章中,我们将学习 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>
从这里我们将学习 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"
}
现在,您可以观察到该文档不再具有原始数据。
如何删除文档
删除函数与文档句柄结合使用,以从集合中删除文档 -
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
