I created an blog-article
model and a blog-category
model. The blog-article
model has a reference field to the blog-category
model.
Now for a category page, I want to get all articles from a specific category.
My response when I get all articles looks like this:
[
{
"lastUpdatedBy": "hLYQDfyMEueOo6257M6ol0fZ0ID2",
"folders": [],
"data": {
"date": 1727384400000,
"category": {
"@type": "@builder.io/core:Reference",
"model": "blog-categories",
"id": "935f64738a504bc48e0508eb95f88882",
"value": {
....
"data": {
"name": "Foo",
"description": "Bar.",
"url": "/foobar"
},
"modelId": "7d6e8a1e7bd64bffa3ccaf225bcb96bd",
....
},
"title": "Foo Bar Article title",
"slug": "foo-bar-article",
"teaser": "A short article teaser",
"blocks": [
Since category
is nested within the actual article: How would I filter for it, to get all articles from a specific category?
I’ve tried:
query: { 'data.category.value.data.url': '/' + route.params.slug },
However: since category is nested within the actual data
, it’s not working. I hope it’s clear and thank you in advance.
Hi,
My name is Veronika and I am a Customer Engineer here at Builder.io.
Thank you for reaching out to our Forum and hopefully I can help answer your question. To filter articles by a specific category in your Builder setup - where the category is nested inside a reference, you’ll need to adjust your query to properly target that field. In Builder, you cannot query inside of a reference.
To recreate this locally, I made a list of categories and keyed into the ids.
This was my query:
{ "query.data.category.id": "50b344f9116e4820a020e382058146e0_2dd46c1332224acb955eeaa8243e75d8" }
.
You can also use:
{ "query.data.category.category.id": { "$in": ["50b344f9116e4820a020e382058146e0_2dd46c1332224acb955eeaa8243e75d8", "50b344f9116e4820a020e382058146e0_c6ec4a2031f945999abaabce116dd8c1"] }}
if you want to match any category ID in an array using the $in
syntax in Mongo.
I believe the query you’re looking for in order to query into all the categories is:
"query.data.category.model" : "blog-category"
or if you’re looking to get the “type”
"query.data.category.@type": "@builder.io/core:Reference"
Let me know if that works for you and if you have any other questions,
Thank you!
To be a little more specific and recreate this locally, I created a list of categories and keyed into the ids.
This was my JSON response:
(I hope it’s not confusing that it’s a list of categories within a category - I wanted to test out further nesting) - but here, I queried all the categories using:
{query.data.category.category.id" : "50b344f9116e4820a020e382058146e0_2dd46c1332224acb955eeaa8243e75d8"}
.
Another approach you can take is:
{"query.data.category.category.model": "blog-category"}