Arangodb 简明教程
ArangoDB - Crud Operations
在本章中,我们将学习使用 Arangosh 执行的不同操作。
In this chapter, we will learn the different operations with Arangosh.
以下是可使用 Arangosh 的可能操作:
The following are the possible operations with Arangosh −
-
Creating a Document Collection
-
Creating Documents
-
Reading Documents
-
Updating Documents
让我们从创建一个新数据库开始。我们将使用以下代码行创建一个新数据库:
Let us start by creating a new database. We will use the following line of code to create a new database −
127.0.0.1:8529@_system> db._createDatabase("song_collection")
true
以下代码行将帮助您切换到新数据库:
The following line of code will help you shift to the new database −
127.0.0.1:8529@_system> db._useDatabase("song_collection")
true
提示将移至“@@song_collection”
Prompt will shift to "@@song_collection"
127.0.0.1:8529@song_collection>
从此处,我们将学习 CRUD 操作。让我们在新数据库中创建一个集合:
From here we will study CRUD Operations. Let us create a collection into the new database −
127.0.0.1:8529@song_collection> db._createDocumentCollection('songs')
Output
[ArangoCollection 4890, "songs" (type document, status loaded)]
127.0.0.1:8529@song_collection>
让我们向“songs”集合中添加一些文档(JSON 对象)。
Let us add a few documents (JSON objects) to our 'songs' collection.
我们以以下方式添加第一个文档:
We add the first document in the following way −
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"})
Output
{
"_id" : "songs/A_Man",
"_key" : "A_Man",
"_rev" : "_VjVClbW---"
}
让我们向数据库添加其他文档。这将帮助我们了解查询数据的过程。您可以复制这些代码并将其粘贴到 Arangosh 中以模拟该过程:
Let us add other documents to the database. This will help us learn the process of querying the data. You can copy these codes and paste the same in Arangosh to emulate the process −
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---"
}
How to Read Documents
可以使用 _key 或文档句柄来检索文档。如果没有必要遍历集合本身,请使用文档句柄。如果您有集合,则文档函数很容易使用:
The _key 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
}
How to Update Documents
可以使用 replace 和 update 更新保存的数据。
Two options are available to update the saved data − replace and update.
update 函数修复一个文档,并将其与给定的属性合并。另一方面,replace 函数将用一个新文档替换以前的文档。即使提供了完全不同的属性,替换仍将发生。我们首先将观察非破坏性更新,更新歌曲中的 Production` 属性:
The update function patches a document, merging it with the given attributes. On the other hand, the replace function will replace the previous document with a new one. The replacement will still occur even if completely different attributes are provided. We will first observe a non-destructive update, updating the attribute Production` in a song −
127.0.0.1:8529@song_collection> db.songs.update("songs/A_Man",{production:
"Top Banana"});
Output
{
"_id" : "songs/A_Man",
"_key" : "A_Man",
"_rev" : "_VjVOcqe---",
"_oldRev" : "_VjVClbW---"
}
现在,让我们读取更新的歌曲的属性:
Let us now read the updated song’s attributes −
127.0.0.1:8529@song_collection> db.songs.document('A_Man');
Output
{
"_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 函数轻松更新大文档,尤其是在属性很少的情况下。
A large document can be easily updated with the update function, especially when the attributes are very few.
相反, replace 函数将在将其用于同一文档时废除您的数据。
In contrast, the replace 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"});
现在,让我们使用以下代码行检查我们刚刚更新的歌曲:
Let us now check the song we have just updated with the following line of code −
127.0.0.1:8529@song_collection> db.songs.document('A_Man');
How to Remove Documents
remove 函数与文档句柄结合使用,可从集合中删除文档:
The remove function is used in combination with the document handle to remove a document from a collection −
127.0.0.1:8529@song_collection> db.songs.remove('A_Man');
现在,让我们使用以下代码行检查我们刚刚删除的歌曲的属性:
Let us now check the song’s attributes we just removed by using the following line of code −
127.0.0.1:8529@song_collection> db.songs.document('A_Man');
我们会在输出中获得一个异常错误,如下 −
We will get an exception error like the following as an output −
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