How to query only documents with an existing localization for a certain locale? #5469
-
We have a blog with posts. Some of the posts are localized. On localized versions of the website, we only want to show posts with localized content. How do we query them? When Our current approach is this: const localizedPostIds = (
await payload.db.collections["blog-posts"]
.find({
[`content.${req.locale}`]: {
$exists: true,
$ne: "",
},
})
.select({
_id: true,
})
).map((e) => e.id);
const posts = (
await payload.find({
collection: "blog-posts",
locale: req.locale,
where: {
_id: {
in: localizedPostIds,
},
},
})
).docs; Is there a better way? Perhaps one that doesn't use two database queries? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I am fairly certain that the query builder does let you pass the It is my expectation that this should work:
To format that into a rest query using query params you're looking at Does that work? |
Beta Was this translation helpful? Give feedback.
I am fairly certain that the query builder does let you pass the
locale
through and that you should be able to just do one query. Also I do not think that querying uses the fallback locale at all. We don't have extra logic that would addor
for the fallback for the properties being queried.It is my expectation that this should work:
To format that into a rest query using query params you're looking at
/api/blog-posts?where[content.es][exists]=true&locale=es
That should return your posts where content is populated in the es locale …