I’m trying to use $or query to implement multiple filter.
I have page model
I tried both JS and REST API
const data = await builder.getAll(model, {
fields: ‘data’,
options: { noTargeting: true },
query: {
‘data.$or’: [
{ title: { $regex: ‘Dave’, $options: ‘i’ } },
{ description: { $regex: ‘expert’, $options: ‘i’ } },
{ tags: { $in: [‘history’] } }
]
}
})
return data;
const data = await fetch(https://cdn.builder.io/api/v3/content/${model}?apiKey=${BUILDER_API_KEY}&query.data.$or={title={ $regex: 'mother', $options: 'i' },{ description: { $regex: 'expert', $options: 'i' } }}
)
const { results } = await data.json();
return results;
please help if this is working. if this is not working is there a way to do this same logic?
Hello @djkenneth ,
Welcome to the builder.io forum post.
For help on using $or please refer to our doc
You may also find help at our forum post
I currently try to use the v3 content /api/v3/content/model-name.
My model has an article with a content and a text and I would like to search for a term in either of the two fields.
Its possible to search in one item using the following api
https://cdn.builder.io/api/v3/content/model-name?limit=1&query.data.text.$regex={searchterm}&apiKey=API_KEY
My question now is, how can I search in either one of the fields.
I found some examples for the v2 api
Fulltext search in custom fields .
Is the…
the reference you send. I check it and we are the same issue. The $or qeury is not wotking and I already check the doc multiple times. I tried every scenario.
query: {
'data.title': { $regex: 'Dave', $options: 'i' },
'data.description': { $regex: 'cook', $options: 'i' }
},
query: {
‘data.$or’: [
{ title: { $regex: ‘Dave’, $options: ‘i’ } },
{ description: { $regex: ‘Dave’, $options: ‘i’ } },
{ tags: { $in: [‘american’] } }
]
}
query: {
‘data.$and’: {
‘title’: { $regex: ‘Dave’, $options: ‘i’ },
‘description’: { $regex: ‘Dave’, $options: ‘i’ },
}
}
https://cdn.builder.io/api/v3/content/${model}?apiKey=${BUILDER_API_KEY}&query.data.title.$regex=Dave&query.data.title.$options=i&query.data.description.$regex=cook&query.data.description.$options=i
I need to use $or or $and query
Hello @djkenneth ,
Here is an example that is working for us
const articles = await builder.getAll('blog-article', {
// Include references, like the `author` ref
options: { includeRefs: true },
limit: articlesPerPage,
query: {
'data': {
"$or": [
{
"title": { "$eq": "Qwik Blog" }
},
{
"title": { "$eq": "Partytown blog" }
}
]
}
}
})
rythm
March 6, 2024, 2:51pm
5
Hi @manish-sharma , please provide the rest api example as well :).
Thanks in advance