> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Querying Items that Reference Other Items

## Article: Querying Items that Reference Other Items

## Article Link: https://dev.wix.com/docs/develop-websites/articles/databases/wix-data/reference-fields/querying-items-that-reference-other-items.md

## Article Content:

# Velo: Querying Items that Reference Other Items

Some collections contain items that reference items that are stored in a different collection. For example, a **Books** collection may contain a **Writer** field, which references an **Authors** collection. Using Velo, you can retrieve referenced items' details using different techniques. This article explains how.

<div style="text-align:center">

<img src="https://d2x3xhvgiqkx42.cloudfront.net/12345678-1234-1234-1234-1234567890ab/76096845-8b12-44f1-91f6-3dc2e838fdd9/2019/09/22/329b4741-7971-4dd6-870c-dc4d3684d0df/d983a15d-c18b-4493-a48a-f0dab45edc26.png" style="max-width:49vw" />

</div>

In this example, a book is the "item" and the author is the "referenced item."

### Retrieving Referenced Item IDs

Using Velo, when you query an item that references other items, by default you receive the IDs of the referenced items in the query's results.

Let's query our Books collection for novels. For each novel, we want to see the referenced author.

Consider the following code that runs the query and displays the results in the table we created above.

```javascript
function theRegularFindExample() {
  // Query the Books collection for novels only
  wixData
    .query("Books")
    .eq("genre", "Novel")
    .find()
    .then((myResults) => {
      // Set up table columns
      $w("#table1").columns = [
        {
          id: "col1",
          dataPath: "title",
          label: "Book",
          width: 100,
          visible: true,
          type: "string",
        },
        {
          id: "col2",
          dataPath: "genre",
          label: "Genre",
          width: 100,
          visible: true,
          type: "string",
        },
        {
          id: "col3",
          dataPath: "writer",
          label: "Author",
          width: 200,
          visible: true,
          type: "richText",
          linkPath: "doc",
        },
      ];

      // Display the query results in the table
      $w("#table1").rows = myResults.items;
    });
}
```

We see that the **Author** field contains ID values. We can't see the author's name, nationality, and so on.

<div style="text-align:center">

![](https://d2x3xhvgiqkx42.cloudfront.net/12345678-1234-1234-1234-1234567890ab/76096845-8b12-44f1-91f6-3dc2e838fdd9/2019/09/25/de82a016-eea8-4612-a11c-21a81c9a5e7c/4edb219a-7571-4fcb-aa78-cf05b17d680b.png)

</div>

To see the referenced information in addition to the IDs, there are other Velo functions we can use: [`queryReferenced()`](https://www.wix.com/velo/reference/wix-data.html#queryReferenced) and [`include()`](https://www.wix.com/velo/reference/wix-data.WixDataQuery.html#include).

At the end of this article, we describe [when to use each of these functions](#when-to-use-each-function).

### Retrieving Referenced Items with queryReferenced()

Use the [`queryReferenced()`](https://www.wix.com/velo/reference/wix-data.html#queryReferenced) function to include referenced items for the specified properties in your query's results. For example, we want to see novels in our Books collections along with the author's name and nationality. The author's name and nationality exist in an Authors collection.

#### Example

Let's query our Books collection again for novels, but this time we want to see the author's name and nationality.

First, let's look at the following code that sets up our table. We're going to take this opportunity to demonstrate how to set up table columns for an object, because we won't be loading rows directly from a collection. We'll add rows to the table using an object that we create instead.

Now let's look at code that runs the query and displays the both the results and the referenced results in our table:

Now we see the **Author** and **Nationality** field values.

<div style="text-align:center">

<img src="https://d2x3xhvgiqkx42.cloudfront.net/12345678-1234-1234-1234-1234567890ab/76096845-8b12-44f1-91f6-3dc2e838fdd9/2019/10/24/2924fee0-b4a7-425f-88b0-b3f49c95b4e7/fc6f56dd-c261-4923-87ad-9bfe744e7400.png" style="max-width:40vw" />

</div>

### Retrieving Referenced Items with include()

Add the [`include()`](https://www.wix.com/velo/reference/wix-data.WixDataQuery.html#include) function to your query chain to include referenced items in your query's results.

This function is a bit simpler to use than `queryReferenced()`. But it does have limitations. Review the [comparison](#when-to-use-each-function) between `queryReferenced()` and `include()` to decide which function to use.

When setting up our columns, we use the period  **.**  notation to access the nested, referenced items. Because the referenced items are in a different collection, we prefix their names with the name of the referencing property from the first collection.

#### Example

Let's query our Books collection again for novels. Again, we want to see the author's name and nationality. This time we will demonstrate how to use `include()` instead of `queryReferenced()`.

Consider the following code that runs the query and displays the results in a table:

```javascript
async function theIncludeExample() {
  wixData
    .query("Books")
    .eq("genre", "Novel")
    .include("writer")
    .find()
    .then((myResults) => {
      if (myResults.items.length > 0) {
        let books = myResults.items;
        $w("#table2").rows = books;
      } else {
        console.log("No results found.");
      }
    });
}
```

Now we see the **Author** and **Nationality** field values.

<div style="text-align:center">

<img src="https://d2x3xhvgiqkx42.cloudfront.net/12345678-1234-1234-1234-1234567890ab/76096845-8b12-44f1-91f6-3dc2e838fdd9/2019/09/25/94e1bd90-2946-4c87-9694-e19287a555aa/f35df6ca-6d1a-4544-a280-b36631a15cd6.png" style="max-width:43vw" />

</div>

See how `include()` is used in a live demo:

`youtube:https://www.youtube.com/watch?v=bGfTcp5hATM`

### When to Use Each Function

This table can help you decide when to use `queryReferenced()` and `include()`.

| Functionality / Limitation                                                                                                                                                      | queryReferenced() | include() |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | --------- |
| **Can reference multiple items in one query**<br><br>If you want to see many items and their referenced data in one query, it's easier to use **include()**.                    | No                | Yes       |
| **Can reference one item that references multiple items in one query**                                                                                                          | Yes               | No        |
| **Can reference multiple items that reference multiple items in one query**                                                                                                     | No                | No        |
| **Can trigger hooks**                                                                                                                                                           | No                | Yes       |
| **Can reference more than 50 items**<br><br>Use **queryReferenced()** if you anticipate more than 50 items as a result of your query, even if you use the **limit()** function. | Yes               | No        |
| **Can list up to 50 referenced items**<br><br>Use **queryReferenced( )** if you anticipate more than 50 referenced items will be returned as a result of your query.            | Yes               | No        |
| **Works with promises**<br><br>Working with promises lets you catch errors and send alerts.                                                                                     | Yes               | No        |

## See also

- [Webinar: Planning for scale: large data design considerations](https://youtu.be/BGnpQ_LTHPg?feature=shared&t=1275)