How do you query z.array()
fields (e.g. tags) in the latest nuxt-content module (v3.alpha.8)
#2955
Replies: 2 comments
-
I have a similar need for filtering tags like this: let query = queryCollection('content').where('tags', 'LIKE', `%${tag}%`) if filter multiple: let query = queryCollection('content')
if (filter_tags.length) {
filter_tags.forEach(tag => {
query = query.where('tags', 'LIKE', `%${tag}%`)
})
}
query.order('date', 'DESC').all() Because I found that the 'tags' field in the SQLite database is merely of type Text. so, it can only be filtered using LIKE... if you have a better way, please tell me 😀 |
Beta Was this translation helpful? Give feedback.
-
While the solution @aatrooox is probably already the best you can get regarding the runtime (correct me if I'm wrong) but I thought I'd share my solution with you, since the solution above works for logical Luckily, there are As an example, with the following code you can filter the content collection which match all the provided tags. const fetchContent = async () => {
const query = queryCollection('content')
if (filter_tags.length) {
query.andWhere(q => {
filter_tags.forEach(tag => q.where('tags', 'LIKE', `%${tag}%`))
return q;
})
}
return await query.all()
} By using either Hope this helps anyone in the future who visits this thread 👍 |
Beta Was this translation helpful? Give feedback.
-
In the previous version of nuxt-content (v2) I would simply query tags like so:
However there is no
contains
in v3, and the logic forIN
seems to require the reverse operation. I've triedLIKE
and that doesn't work either.Definitely need to know the best way to find values in z.array() field types.
As it stands, I'm forced to retrieve all, then run JS
.filter()
on the results. Quite inefficient.Beta Was this translation helpful? Give feedback.
All reactions