I am working on a Waterline query which filters objects by a collection attribute. In this simplified example, I have two models, Video
and Category
:
// Video.js
module.exports = {
attributes: {
title: {
type: 'string'
},
categories: {
collection: 'Category',
via: 'videos'
}
}
};
// Category.js
module.exports = {
attributes: {
name: {
type: 'string'
},
videos: {
collection: 'Video',
via 'categories'
}
}
};
I want to find all the videos which are associated with a certain category. I have the category id stored in a variable named categoryID
and am trying this query:
Video.find('categories': categoryID).exec(function (err, videos) {
// videos should contain all videos associated with the categoryID
});
However, I always end up with an empty result, even though there are videos associated with the category for which I am looking. I know that waterline does currently not support deep queries for values in collection attributes, but I thought at least a query for the id of the object would work. Am I wrong?
If so, is there an alternative way to achieve the desired result without using native queries?
I am aware that I could add a collection attribute to Category
and build my query from the Category
side. However, this is only the beginning of a more complicated search where I also narrow down the result using other attributes stored in a Video
object, such as the user id of the creator of a video. In the end, I iterate through the video results using pagination. So I am looking for a way to retrieve videos of certain category which can be combined with other search attributes stored in a Video
object.