Lookup relations

In a restdb.io database, you can create two types of relations between collections: lookups and parent-child.

This page shows how you can create lookup relations between two collections.

What is a lookup relation?

A "lookup" in restdb.io is a kind of one-to-one or one-to-many relation.

They are ideal to use when there are one or a few data items that need to be associated between two collections. A lookup relation allows multiple records to reference the same record(s) (unlike parent-child). Also, when you have large numbers of data items, we recommend using a parent-child relation. This is due to the fact that lookup id's are embedded directly on a record and the database engine track changes in relations and queue updates on all data accordingly (see below for details about the JSON storage format).

How to create lookup relations

Lookup relations are added in the same way as fields. In Developer Mode, on the collection view, you click the Add Field+ button. In the slide-in dialog that shows up, you scroll down to the section Relations to Collections and select a collection. Here we'll select Company.

create-lookup

The lookup relations are the select one (collection name) and the select many (collection name). For this example, we choose "select one company" as shown below.

create-lookup-2

Lookup relations in use

Lookups are similar to HTML selects. In restdb.io they are made mobile friendly and slides in from the right. The selection list has a search filter and you can also create a new record in the collection the lookup links to (here: Company).

use-lookup

Two way associations

When you click on a lookup link on a record, you navigate directly to the record. A convenient feature is that when you visit a record being linked to by another, you'll see a tab for each lookup relation it takes part in. Here we see the tab Contact which links to this Company. Clicking on the tab will reveal a list of records (only one in this case).

use-lookup

JSON storage format

A lookup is stored as an embedded object on the record. This is convenient and saves extra REST API calls to get the company record details. To see the JSON of a document, simply click the JSON data view button as shown here:

json-lookup

Please note that a lookup is always stored as an array, even if it's a select-one relation

Lookups and the REST API

Lookups can also be set with the Rest API. Both POST and PUT operations can have lookup ID values in them. For example, creating a new record where one field is a lookup to another Collection item:

POST
...
{
    "name": "jane", 
    "company": ["5811d3a27630542000000293"]
}

restdb.io automatically replaces the lookup ID with a copy of the object:

{
    "name": "jane", 
    "company": [
        {
            "_id": "5811d3a27630542000000293", 
            "companyname": "Acme", 
            "Address": "xxx", 
            ...
        }
    ]
}

Lookups to lookups are limited to 2 levels on the embedded target object.