I have the Video model (Waterline)
Video
-title
-description
-matches (array)
[0]
- MatchTitle
- MatchDescription
[1]
...
[N]
How can I find videos which contains matches with title "ABC" for example ?
I wrote not the best solution, because it retrive all data from database
Video.find({is_html5:true}).done(function(err, videos) { // get all videos
async.forEach(videos, function(video, callback) {
var isFound = false;
async.forEach(video.matches, function(videoMatch, matchCallback) { // get video matches
if(videoMatch.gs_id === match.gs_id && !isFound) {
match.relatedVideos.push(video.id);
isFound = true;
}
matchCallback();
}, function(err3) {
callback();
});
}, function(err2) {
cb();
});
}, function(err1) {
cb();
});
How about to use some mongo's magic and find Video by array item's property?
Video.find({'matches.matchTitle' : 'ABC'}).exec(...);
// or using regex (if you want to match a substring)
Video.find({'matches.matchTitle' : { $regex: /ABC.*/ }}).exec(...);
How that helps.