Skip to main content

Creating a Collection


Every transaction that you issue needs to be inside of square brackets, [ and ], and every subject that you are creating or updating needs to be inside of curly brackets {, and }. Each of those objects inside of curly brackets, will have multiple key-value pairs. The order of those key-value pairs does not matter, but each object needs to have an _id key.

_ids and TempIds#

If you are updating a subject, the value of the _id key can either refer to an existing subject (either by the long integer _id or by a unique two-tuple). If you are adding a new subject, the value of the _id key needs to be the name of the collection that that new subject belongs to.

For example, if we were creating a new actor, our transaction would contain "_id": "actor". When creating a new subject, you can also add a temporary id by appending the collection name with any invalid character (any character other than a-z, A-Z, 0-9, _) and the temporary id.

For example, if I were creating several new actors, my transaction might contain, "_id": "actor$1" and "_id": "actor$secondActor".

When you create a collection, there are many predicates that you can specify for each collection. The full list of built-in predicates can be found in the docs.

Predicates in a Transaction#

Other than the _id key, we need to include any other predicate that we want to add or update as an additional key.

For example, if I'm creating a new _collection, I need to have "_id": "_collection" as a key-value pair in my transaction. If I also want to specify a _collection/name, then I can use _collection/name as a key and the name as the value. The transaction might look like this:

[{    "_id": "_collection",    "_collection/name": "person"}]

Note that some collections start with _, like _collection, _predicate, _rule, etc. These collections are built-in system collections.

If we want to specify both a name and _collection/doc for our new collection, we could issue the following transaction:

[{    "_id": "_collection$personCollection",    "name": "person",    "doc": "Collection to hold all people"}]

As you can see above, if the predicates you are adding belong to the same collection as the subject you are creating (they usually do, but don't have to), then you can omit the collection name (i.e doc and name instead of _collection/doc and _collection/name).

The above collection also contains a temporary id, personCollection. This tempid is not particularly useful in this transaction, but you'll see later on in the lessons how these can be useful.

Multiple Transaction Items#

We can add as many transaction items to our transaction as we want, for example to create two new collections with names: person and chat, our transaction would look as follows:

[{    "_id": "_collection$personCollection",    "name": "person",    "doc": "Collection to hold all people"},{    "_id": "_collection",    "name": "chat",    "doc": "Collection to for all chats"}]

Create 4 Collections: person, chat, comment, artist

Write a transaction that creates 4 collections, complete with collection names and docs.

Try typing your own solution first before clicking "Get Solution" to check your answer.