We are excited to announce the addition of the include() function to the wix-data API. The include() function is used when querying a collection that contains reference fields.
Previously, when you queried such a collection, you would only receive the IDs of referenced items in the query results. Now, using the include() function, you can retrieve the entire reference item in the query results.
How it works
Simply add the include() function to your query building chain before calling the find() function.
Example
Suppose you have a collection of books that contains a reference field that refers to the book’s author data, which is stored in another collection. You could write the following query to find all the books over 250 pages and receive the full author information in the results.
import wixData from 'wix-data';
// ...
wixData.query("books")
.ge("pages", 250)
.include("author")
.find()
.then( (results) => {
let books = results.items;
let firstBook = items[0];
let firstAuthor = firstBook.author.name;
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
Here, a single one of the resulting items will look something like this.
{
"_id":"37d9a27a-6b13-4c8d-ac40-5ce8dac7a64f",
"_owner":"f6c0f9c2-a02d-4e9f-a58d-99729af244d9",
"_createdDate":"2018-02-07T13:40:40.765Z",
"_updatedDate":"2018-02-07T13:46:47.132Z",
"title":"Amazing Book",
"publisher":"A Publishing House",
"pages":"375",
"author": {
"_id":"c4cb45bc-bece-4ef7-bfee-e47c5b6f9d8e",
"_owner":"f6c0f9c2-a02d-4e9f-a58d-99729af244d9",
"_createdDate":"2018-02-07T13:40:05.542Z",
"_updatedDate":"2018-02-07T13:40:05.542Z",
"name":"John Doe",
"dob":"1963-04-08T00:00:00.000Z",
"bio":"John is a prolific author and lives in NYC with his six dogs."
}
}
Learn more
To learn more, see the include() function in the API Reference.
Hi @Yoav (Wix) I can't get this to work with my multiple reference field... I see in the API doc that it says that " Only one property with multiple references can be included. " I took that to mean that it would only work with one multiple reference field, but does it actually mean that a query can only show one result with multiple references? Am I doing something wrong? Please help if you can...
This is what I'm trying to accomplish...
import wixData from 'wix-data'; $w.onReady(function () { $w("#searchedrep").onItemReady(($item, itemData, index) => { $item("#event").text = itemData.relatedjob; $item("#description").html = itemData.jobdescription.substring(0, 225) + '...'; $item("#thumbnail").src = itemData.thumbnail; $item("#thumbnail").link = itemData['link-EventPhotos-relatedjob']; $w("#thumbnail").clickAction = "link"; }) }); export function searchbutton_click_1(event) { wixData.query("EventPhotos") .include("categories") .contains("relatedjob", $w("#searchbox").value) .or(wixData.query("EventPhotos") .contains("jobdescription", $w("#searchbox").value)) .or(wixData.query("EventPhotos") .contains("categories", $w("#searchbox").value)) .or(wixData.query("EventPhotos") .contains("tags", $w("#searchbox").value)) .find() .then((results) => { $w("#searchedrep").data = results.items; $w("#searchedrep").expand(); }); }
Hi, is there a way to get this to work when a you allow multiple references for a single data point? Can you please explain it if there is? I can't seem to get it to work.
Hey Yoav, I', trying to get this to work for a drop down menu but i'm having some problems. could you point me in the right direction?
The dataset I am using is called 'Articles' and the reference category within the database is called 'category' I had the whole thing working but it was displaying the reference category id's instead of the title. once I added in the include() function the dropdown stopped displaying anything... it wouldn't even drop down. I'm no doubt overlooking something so any and all help would be greatly appreciated.
$w.onReady(function () {
loadCategories();
});
let lastFilterTitle;
let lastFilterCat;
let debounceTimer;
export function searchbox_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w('#searchbox').value, lastFilterCat);
}, 200);
}
export function filter_change(event, $w) {
filter(lastFilterTitle, $w('#filter').value);
}
function filter(searchQues, category) {
if (lastFilterTitle !== searchQues|| lastFilterCat !== category) {
let newFilter = wixData.filter();
if (searchQues)
newFilter = newFilter.contains("searchQues", searchQues);
if (category)
newFilter = newFilter.eq("category",category);
$w("#Articles").setFilter(newFilter);
lastFilterTitle = searchQues;
lastFilterCat = category;
}
}
function loadCategories() {
wixData.query('Articles')
.include("category")
.find()
.then(res => {
let options = [{"value": '', 'label': 'All Research'}];
options.push(...res.items.map(category => {
return {'value' : category.category, 'label' : category.category.title};
}));
$w("#filter").options = options;
});
}