mirror of https://gitee.com/bigwinds/arangodb
301 lines
17 KiB
HTML
301 lines
17 KiB
HTML
<html><head><title>ArangoDB Manual</title> <style media="screen" type="text/css" style="display:none">body{background-color:white;font:13px Helvetica,arial,freesans,clean,sans-serif;line-height:1.4;color:#333;}#access{font-size:16px;margin-left:12px;display:block;margin-left:10px;margin-right:10px;background-color:#F3F1EE!important;}#access a{border-right:1px solid #DBDEDF;color:#A49F96;display:block;line-height:38px;padding:0 10px;text-decoration:none;}#navigation ul{text-transform:uppercase;list-style:none;margin:0;}#navigation li{float:left;position:relative;}#container{width:920px;margin:0 auto;}a{color:#4183C4;text-decoration:none;}.contents h2{font-size:24px;border-bottom:1px solid #CCC;color:black;}.contents h1{font-size:33px;border-bottom:1px solid #CCC;color:black;}.clearfix:after{content:".";display:block;clear:both;font-size:0;height:0;visibility:hidden;}/**/ *:first-child+html .clearfix{min-height:0;}/**/ * html .clearfix{height:1%;}</style></head><body><div id="container"><img src="images/logo_arangodb.png" width="397" height="67" alt="ArangoDB"><div id="access" role="navigation"><div id="navigation"><ul id="menu-ahome" class="menu"><li><a href="Home.html">Table of contents</a></li> <li><a href="http://www.arangodb.org">ArangoDB homepage</a></li></ul></div><div class="clearfix"></div></div><div>
|
|
<!-- Generated by Doxygen 1.7.5.1 -->
|
|
</div>
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<h1>Handling Collections </h1> </div>
|
|
</div>
|
|
<div class="contents">
|
|
<div class="textblock"><p>This is an introduction to ArangoDB's interface for collections and how handle collections from the JavaScript shell <code>arangosh</code>. For other languages see the corresponding language API.</p>
|
|
<p>The most import call is the call to create a new collection, see <a class="el" href="ShellCollection.html#ShellCollectionCreate">_create</a>.</p>
|
|
<hr/>
|
|
<ul>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html">Handling Collections</a> <ul>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionIntro">Collections</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionResource">Address of a Collection</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionShell">Working with Collections</a> <ul>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionCollectionMethods">Collection Methods</a> <ul>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionDrop">collection.drop</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionTruncate">collection.truncate</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionProperties">collection.properties</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionFigures">collection.figures</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionLoad">collection.load</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionUnload">collection.unload</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionUnload">collection.rename</a> </li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionDatabaseMethods">Database Methods</a> <ul>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionRead">db._collection</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionCreate">db._create</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionReadAll">db._collections</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionReadShortCut">db.collection-name</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionDropDb">db._drop</a> </li>
|
|
<li>
|
|
<a class="el" href="ShellCollection.html#ShellCollectionTruncateDb">db._truncate</a> </li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<hr/>
|
|
<h2><a class="anchor" id="ShellCollectionIntro"></a>
|
|
Collections</h2>
|
|
<p><b>Collection</b>: A collection consists of documents. It is uniquely identified by it's collection identifier. It also has a unique name. </p>
|
|
<p><b>Collection Identifier</b>: A collection identifier identifies a collection in a database. It is an integer and is unique within the database. </p>
|
|
<p><b>Collection Name</b>: A collection name identifies a collection in a database. It is an string and is unique within the database. Unlike the collection identifier it is supplied by the creator of the collection. The collection name can consist of letters, digits and the characters <code>_</code> (underscore) and <code>-</code> (dash). However, the first character must be a letter. </p>
|
|
<h2><a class="anchor" id="ShellCollectionResource"></a>
|
|
Address of a Collection</h2>
|
|
<p>All collections in ArangoDB have an unique identifier. This collection identifier identifies a collection and is managed by ArangoDB. Each collection, in addition, has a unique name. This name is managed by the user. The interface allows you to access the collections as:</p>
|
|
<p><code>db._collection(<em>collection-identifier</em>)</code></p>
|
|
<p>or</p>
|
|
<p><code>db._collection(<em>collection-name</em>)</code></p>
|
|
<p>A collection is created by a <code>db._create</code> call, see <a class="el" href="ShellCollection.html#ShellCollectionCreate">_create</a>.</p>
|
|
<p>For example: Assume that the collection identifier is <code>7254820</code> and the name is <code>demo</code>, then the collection can be accessed as:</p>
|
|
<p><code>db._collection("demo")</code></p>
|
|
<p>or</p>
|
|
<p><code>db._collection(7254820)</code></p>
|
|
<p>If no collection with such a name or identifier exists, then <code>null</code> is returned.</p>
|
|
<p>There is a short-cut</p>
|
|
<p><code>db.<em>collection-name</em></code></p>
|
|
<p>This call will either return the collection named <em>collection-name</em> or create a new one with that name and a set of default properties.</p>
|
|
<h2><a class="anchor" id="ShellCollectionShell"></a>
|
|
Working with Collections</h2>
|
|
<h3><a class="anchor" id="ShellCollectionCollectionMethods"></a>
|
|
Collection Methods</h3>
|
|
<p><a class="anchor" id="ShellCollectionDrop"></a> <hr/>
|
|
<code><b><em>collection</em>.drop()</b></code><hr/>
|
|
Drops a <em>collection</em> and all its indexes.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Drops a collection:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 109757, "examples" (status unloaded)]
|
|
arango> col.drop()
|
|
arango> col;
|
|
[ArangoCollection 109757, "examples" (status deleted)]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionTruncate"></a> <hr/>
|
|
<code><b><em>collection</em>.truncate()</b></code><hr/>
|
|
Truncates a <em>collection</em>, removing all documents but keeping all its indexes.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Truncates a collection:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 91022, "examples" (status new born)]
|
|
arango> col.save({ "Hallo" : "World" });
|
|
{ "_id" : "91022/1532814", "_rev" : 1532814 }
|
|
arango> col.count();
|
|
1
|
|
arango> col.truncate();
|
|
arango> col.count();
|
|
0
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionProperties"></a> <hr/>
|
|
<code><b><em>collection</em>.properties()</b></code><hr/>
|
|
Returns an object containing all collection properties.</p>
|
|
<ul>
|
|
<li><code>waitForSync</code>: If <code>true</code> creating a document will only return after the data was synced to disk.</li>
|
|
</ul>
|
|
<ul>
|
|
<li><code>journalSize</code> : The size of the journal in bytes.</li>
|
|
</ul>
|
|
<hr/>
|
|
<code><b><em>collection</em>.properties(<em>properties</em>)</b></code><hr/>
|
|
Changes the collection properties. <em>properties</em> must be a object with one or more of the following attribute(s):</p>
|
|
<ul>
|
|
<li><code>waitForSync</code>: If <code>true</code> creating a document will only return after the data was synced to disk.</li>
|
|
</ul>
|
|
<ul>
|
|
<li><code>journalSize</code> : The size of the journal in bytes.</li>
|
|
</ul>
|
|
<p>Note that it is not possible to change the journal size after the journal or datafile has been created. Changing this parameter will only effect newly created journals. Also note that you cannot lower the journal size to less then size of the largest document already stored in the collection.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Read all properties</p>
|
|
<div class="fragment"><pre class="fragment">arango> db.examples.properties()
|
|
{ "waitForSync" : false, "journalSize" : 33554432 }
|
|
</pre></div><p>Change a property</p>
|
|
<div class="fragment"><pre class="fragment">arango> db.examples.properties({ waitForSync : false })
|
|
{ "waitForSync" : false, "journalSize" : 33554432 }
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionFigures"></a> <hr/>
|
|
<code><b><em>collection</em>.figures()</b></code><hr/>
|
|
Returns an object containing all collection figures.</p>
|
|
<ul>
|
|
<li><code>alive.count</code>: The number of living documents.</li>
|
|
<li><code>alive.size</code>: The total size in bytes used by all living documents.</li>
|
|
<li><code>dead.count</code>: The number of dead documents.</li>
|
|
<li><code>dead.size</code>: The total size in bytes used by all dead documents.</li>
|
|
<li><code>dead.deletion</code>: The total number of deletion markers.</li>
|
|
<li><code>datafiles.count</code>: The number of active datafiles.</li>
|
|
<li><code>datafiles.fileSize</code>: The total filesize of the active datafiles.</li>
|
|
<li><code>journals.count</code>: The number of journal files.</li>
|
|
<li><code>journals.fileSize</code>: The total filesize of the journal files.</li>
|
|
</ul>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> db.demo.figures()
|
|
{ "numberDatafiles" : 1, "numberAlive" : 1, "numberDead" : 1, "sizeAlive" : 24, "sizeDead" : 24, "numberDeletion" : 1 }
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionLoad"></a> <hr/>
|
|
<code><b><em>collection</em>.load()</b></code><hr/>
|
|
Loads a collection into memory.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.example;
|
|
[ArangoCollection 164208316, "example" (status unloading)]
|
|
arango> col.load();
|
|
arango> col;
|
|
[ArangoCollection 164208316, "example" (status loaded)]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionUnload"></a> <hr/>
|
|
<code><b><em>collection</em>.unload()</b></code><hr/>
|
|
Starts unloading a collection from memory. Note that unloading is deferred until all query have finished.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.example;
|
|
[ArangoCollection 164208316, "example" (status loaded)]
|
|
arango> col.unload();
|
|
arango> col;
|
|
[ArangoCollection 164208316, "example" (status unloaded)]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionRename"></a> <hr/>
|
|
<code><b><em>collection</em>.rename(<em>new-name</em>)</b></code><hr/>
|
|
Renames a collection using the <em>new-name</em>. The <em>new-name</em> must not already be used for a different collection. If it is an error is thrown.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> c = db.example;
|
|
[ArangoCollection 68519, "example" (status new born)]
|
|
arango> c.rename("better-example");
|
|
arango> c;
|
|
[ArangoCollection 68519, "better-example" (status new born)]
|
|
</pre></div> <h3><a class="anchor" id="ShellCollectionDatabaseMethods"></a>
|
|
Database Methods</h3>
|
|
<p><a class="anchor" id="ShellCollectionRead"></a> <hr/>
|
|
<code><b>db._collection(<em>collection-identifier</em>)</b></code><hr/>
|
|
Returns the collection with the given identifier or null if no such collection exists.</p>
|
|
<hr/>
|
|
<code><b>db._collection(<em>collection-name</em>)</b></code><hr/>
|
|
Returns the collection with the given name or null if no such collection exists.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Get a collection by name:</p>
|
|
<div class="fragment"><pre class="fragment">arango> db._collection("demo");
|
|
[ArangoCollection 145387, "demo" (status loaded)]
|
|
</pre></div><p>Get a collection by id:</p>
|
|
<div class="fragment"><pre class="fragment">arango> db._collection(145387);
|
|
[ArangoCollection 145387, "demo" (status loaded)]
|
|
</pre></div><p>Unknown collection:</p>
|
|
<div class="fragment"><pre class="fragment">arango> db._collection("unknown")
|
|
null
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionCreate"></a> <hr/>
|
|
<code><b>db._create(<em>collection-name</em>)</b></code><hr/>
|
|
Creates a new document collection named <em>collection-name</em>. If the collection name already exists, then an error is thrown. The default value for <code>waitForSync</code> is <code>false</code>.</p>
|
|
<hr/>
|
|
<code><b>db._create(<em>collection-name</em>, <em>properties</em>)</b></code><hr/>
|
|
<em>properties</em> must be an object, with the following attribues:</p>
|
|
<ul>
|
|
<li><code>waitForSync</code> (optional, default <code>false</code>): If <code>true</code> creating a document will only return after the data was synced to disk.</li>
|
|
</ul>
|
|
<ul>
|
|
<li><code>journalSize</code> (optional, default is a <a class="el" href="CommandLine.html#CommandLineArango">configuration parameter</a>): The maximal size of a journal or datafile. Note that this also limits the maximal size of a single object. Must be at least 1MB.</li>
|
|
</ul>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>With defaults:</p>
|
|
<div class="fragment"><pre class="fragment">arango> c = db._create("cars");
|
|
[ArangoCollection 111137, "cars" (status loaded)]
|
|
arango> c.properties()
|
|
{ "waitForSync" : false, "journalSize" : 33554432 }
|
|
</pre></div><p>With properties:</p>
|
|
<div class="fragment"><pre class="fragment">arango> c = db._create("cars", { waitForSync : true, journalSize : 1024 * 1204 });
|
|
[ArangoCollection 96384, "cars" (status loaded)]
|
|
arango> c.properties()
|
|
{ "waitForSync" : true, "journalSize" : 1232896 }
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionReadAll"></a> <hr/>
|
|
<code><b>db._collections()</b></code><hr/>
|
|
Returns all collections of the given database.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> db.examples.load();
|
|
arango> var d = db.demo;
|
|
arango> db._collections();
|
|
[[ArangoCollection 96393, "examples" (status loaded)], [ArangoCollection 1407113, "demo" (status new born)]]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionReadShortCut"></a> <hr/>
|
|
<code><b>db.<em>collection-name</em></b></code><hr/>
|
|
Returns the collection with the given <em>collection-name</em>. If no such collection exists, create a collection named <em>collection-name</em> with the default properties.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<div class="fragment"><pre class="fragment">arango> db.examples;
|
|
[ArangoCollection 110371, "examples" (status new born)]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionDropDb"></a> <hr/>
|
|
<code><b>db._drop(<em>collection</em>)</b></code><hr/>
|
|
Drops a <em>collection</em> and all its indexes.</p>
|
|
<hr/>
|
|
<code><b>db._drop(<em>collection-identifier</em>)</b></code><hr/>
|
|
Drops a collection identified by <em>collection-identifier</em> and all its indexes. No error is thrown if there is no such collection.</p>
|
|
<hr/>
|
|
<code><b>db._drop(<em>collection-name</em>)</b></code><hr/>
|
|
Drops a collection named <em>collection-name</em> and all its indexes. No error is thrown if there is no such collection.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Drops a collection:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 109757, "examples" (status unloaded)]
|
|
arango> db._drop(col)
|
|
arango> col;
|
|
[ArangoCollection 109757, "examples" (status deleted)]
|
|
</pre></div><p>Drops a collection identified by name:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 85198, "examples" (status new born)]
|
|
arango> db._drop("examples");
|
|
arango> col;
|
|
[ArangoCollection 85198, "examples" (status deleted)]
|
|
</pre></div> <p><a class="anchor" id="ShellCollectionTruncateDb"></a> <hr/>
|
|
<code><b>db._truncate(<em>collection</em>)</b></code><hr/>
|
|
Truncates a <em>collection</em>, removing all documents but keeping all its indexes.</p>
|
|
<hr/>
|
|
<code><b>db._truncate(<em>collection-identifier</em>)</b></code><hr/>
|
|
Truncates a collection identified by <em>collection-identified</em>. No error is thrown if there is no such collection.</p>
|
|
<hr/>
|
|
<code><b>db._truncate(<em>collection-name</em>)</b></code><hr/>
|
|
Truncates a collection named <em>collection-name</em>. No error is thrown if there is no such collection.</p>
|
|
<p><b>Examples</b><br/>
|
|
</p>
|
|
<p>Truncates a collection:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 91022, "examples" (status new born)]
|
|
arango> col.save({ "Hallo" : "World" });
|
|
{ "_id" : "91022/1532814", "_rev" : 1532814 }
|
|
arango> col.count();
|
|
1
|
|
arango> db._truncate(col);
|
|
arango> col.count();
|
|
0
|
|
</pre></div><p>Truncates a collection identified by name:</p>
|
|
<div class="fragment"><pre class="fragment">arango> col = db.examples;
|
|
[ArangoCollection 91022, "examples" (status new born)]
|
|
arango> col.save({ "Hallo" : "World" });
|
|
{ "_id" : "91022/1532814", "_rev" : 1532814 }
|
|
arango> col.count();
|
|
1
|
|
arango> db._truncate("examples");
|
|
arango> col.count();
|
|
0
|
|
</pre></div> </div></div>
|
|
</div></body></html>
|