Monday 12 July 2021

ArangoDB: Create a collection

 

' db._create(collection-name, properties)' method is used to create a collection. This method thrown an error if the collection with given name is already exists.

 

Signature

db._create(collection-name)

db._create(collection-name, properties)

db._create(collection-name, properties, type)

db._create(collection-name, properties[, type], options)

 

 

 ‘properties’  argument is an object with following attributes.

 

Attribute

Description

Default value

waitForSync

This is optional attribute.

 

If it is set to true, document creation call will return only after the data was synced to disk.

false

isSystem

This is optional attribute.

 

This attribute should be set to true to create a system collection. As per the convention, system collecitons should start with _ (Ex: _analyzers, _apps).

 

false

keyOptions

This is optional attribute.

 

keyOptions is a JSON document specify additional options for key generation.

 

This json document contains following attributed.

a.   type: Specifies the type of the key generator. Possible values are traditional, autoincrement, uuid and padded. 'traditional' generator generates numerical kes in ascending order. 'autoincrement' key generator generates numerical keys in ascending order, the inital offset and the spacing can be configured. The padded key generator generates keys of a fixed length (16 bytes) in ascending lexicographical sort order. The uuid key generator generates universally unique 128 bit keys.

b.   allowUserKeys: If it is set to true, then user is allowed to supply own key values in the _key attribute of a document. If it is set to false, then key generator will generate a key at the time of document creation.

c.    offset: Starting offset value of auto increment key generator.

 

numberOfShards

This is optional attribute.

 

Specifies the number of shards created for a collection.

1

shardKeys

Specifies which document attributes are used to find out the shard for a document.

 

How the shard calculated?

ArangoDB calculates a hash value to all the values of shard key attributes in a document. This hash value is used to identify the shard.

 

Since shard key attributes are used to find out shard, these must be immutable.

_key

replicationFactor

This is optional attribute.

 

Specifies how many copies of each shard are kept on different DB-Servers.

1

writeConcern

This is optional attribute.

 

This attribute specifies the copies of each shard are required to be in sync on the different DB-Servers.

1

distributeShardsLike

If this value is set, ArangoDB copies the attributes replicationFactor, numberOfShards and shardingStrategy from the other collection.

 

shardingStrategy

This is optional attribute.

 

Once sharding strategy is selected, it will never change.

 

Following sharding strategies are supported.

 

a.   community-compat

b.   enterprise-compat

c.    enterprise-smart-edge-compat

d.   hash

e.   enterprise-hash-smart-edge

hash

smartJoinAttribute

This attribute determines an attribute of the collection that must contain the shard key value of the referred-to SmartJoin collection.

 

 

type argument

db._create(collection-name, properties, type)

 

‘type’ argument specifies the type of a collection. Type can be either edge or document.

 

db._create(collection-name, properties[, type], options)

‘options’ is a json object with following attributes.

 

Attribute

Description

Default

waitForSyncReplication

When this flag is set, then server will return success only when all the replicas are created for the collection.

true

enforceReplicationFactor

When enabled, server will will check whether there is enough replicas available at creation time or not.

true

 

 

Let’s try it with an example.

 

Step 1: Login to arango shell.

$arangosh
Please specify a password: 

                                       _     
  __ _ _ __ __ _ _ __   __ _  ___  ___| |__  
 / _` | '__/ _` | '_ \ / _` |/ _ \/ __| '_ \ 
| (_| | | | (_| | | | | (_| | (_) \__ \ | | |
 \__,_|_|  \__,_|_| |_|\__, |\___/|___/_| |_|
                       |___/                 

arangosh (ArangoDB 3.7.11 [darwin] 64bit, using build , VPack 0.1.33, RocksDB 6.8.0, ICU 64.2, V8 7.9.317, OpenSSL 1.1.1k  25 Mar 2021)
Copyright (c) ArangoDB GmbH

Command-line history will be persisted when the shell is exited. You can use `--console.history false` to turn this off
Connected to ArangoDB 'http+tcp://127.0.0.1:8529, version: 3.7.11 [SINGLE, server], database: '_system', username: 'root'

Type 'tutorial' for a tutorial or 'help' to see common examples
127.0.0.1:8529@_system>

 

Step 2: Create and use the database demo.

127.0.0.1:8529@_system> db._createDatabase("demo")
true

127.0.0.1:8529@_system> db._useDatabase("demo")
true

127.0.0.1:8529@demo> 

Step 3: Create a collection user of type document and project of type edge.

127.0.0.1:8529@demo> db._create("user", {}, "document");
[ArangoCollection 8240, "user" (type document, status loaded)]

127.0.0.1:8529@demo> db._create("project", {}, "edge");
[ArangoCollection 8263, "project" (type edge, status loaded)]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment