app.get('/all-images', async (req, res) => {
const searchQuery = req.query.searchQuery || '';
if (!searchQuery) {
return res.status(400).json({ error: 'Search query is required' });
}
const findingResult = await driveImagesCollection.find({
// find the search query both from name, description and keywords field
$or: [
{ "imageUrls.name": { $regex: searchQuery, $options: 'i' } }, // $options:'i' --> its mean case insensitive
{ "imageUrls.description": { $regex: searchQuery, $options: 'i' } },
{ "imageUrls.keyWords": { $regex: searchQuery, $options: 'i' } },
]
}).toArray();
// Flatten the findingResult to include all matching imageUrls
const matchedImages = findingResult.flatMap(doc =>
doc.imageUrls.filter(image => {
// it filter the searching query both the name, description and keyWords field
return image?.name.toLowerCase().includes(searchQuery.toLowerCase()) || image?.description.toLowerCase().includes(searchQuery.toLowerCase()) || image?.keyWords.toLowerCase().includes(searchQuery.toLowerCase())
})
);
res.send(matchedImages)
})
Post a Comment