| 0 | kirasiris | Categories: Tags: , , , , , , , ,

NodeJS s3 keyword

So you have a JSON document with a keyword that looks similar to this:

"title": "Hola a todos!!!",
"text": "aa",
"term_id": [

Case scenario…looking for the keyword

Lets imagine we’re looking for a boxing match video and you need to find it so you can show it to your friends. MongoDB has the method of find() which usually just looks through the first level fields and mostly makes the input string to forcefully match the value found in the document.

We don’t want that, for God’s sake, I can not remember where the fight is taking place or who the commentators? are. What I do remember is a name or just part of it to be more specific. In our case scenario, we have the Mexican fighter ‘Canelo Alvarez’ vs ‘Some dude’ but we’re only looking for the keyword ‘ne’ found in Canelo’s Alvarez name.

The logic now becomes complex and requires some thinking along with REGEX!.

What is Regex?

regular expression (shortened as regex or regexp;[1] also referred to as rational expression)[2][3] is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation

WikiPedia Article

When trying to look for any document that matches the kerword ‘ne’, we don’t want it to not fetch anything but to return ‘Canelo’ and any other figther whose names have these two letters on them. Now we know that ‘title’ is what we’re looking for.

The easies way to do this is by using the $regex operator given by MongoDB. An example of how to use

query.title: { $regex: keyword, $options: 'i' }
// Where query is initially an empty object.
// and we just append title to it so we let MongoDB
// what field to use when looking for X keyword.
// ************************************************
// keyword comes from the keyword string in the url
// you're using for this functionality
// example.com/search/?keyword=anime - NOTE: THIS IS IMPORTANT

To add more fields to look for, you only add a second line or simply use the $or operator.

Now comes the handy $or operator which can take an array of objects in which you specify what fields to use. Here it is:

query = {
    $or: [
      { title: { $regex: keyword, $options: 'i' } },
      { text: { $regex: keyword, $options: 'i' } },
      { term_id: { $regex: keyword, $options: 'i' }}

On the code above we’re looking for any character that matches ‘ne’. We could easily say the title is ‘Canelo vs xDude’, the text is ‘The next big match of 2020″ and for whatever reason two term_id, ‘aneme’ and ‘nene’?.

This is what you would end up with, an easy and fast function using what has been discussed so far:

exports.searchVideos = async (req, res, next) => {
  let query = {};
  const { keyword } = req.query;

  query = {
    $or: [
      { title: { $regex: keyword, $options: 'i' } },
      { text: { $regex: keyword, $options: 'i' } },
      { term_id: { $regex: keyword, $options: 'i' }}

  const doc = await YourModelOrCollection.find(query);

  res.status(200).json({ success: true, data: doc });

If you’re smart enough, this can be implemented inone of the functions from this post:

Don’t forget to check my previous NodeJs post:

I hope to have worded this post to the best of my knowledge and yes, I know a boxing match was not a good example but it was the first thing that came to my mind xD.

That is it, thanks for stoping by. Bye-Bye 🙂 .

Leave a Reply

Back to Top