skip to Main Content

I am creating a Movie Search Bot using NodeJS and MongoDB. I use Following Code to Search movies.

const { MongoClient } = require('mongodb');

async function main() {

  const uri = "mongodb+srv://******************";
  const client = new MongoClient(uri);
  const mydb = client.db("Cluster0").collection("-100****");

  try {
    await client.connect();

    await myFunc(mydb, "inputW1", "inputW2");

  } finally {
    await client.close();
  }
}

main().catch(console.error);

async function myFunc(mydb, word1, word2) {

  const projection = { _id: 0, file_name: 1, file_size: 1, link: 1 };
  const query = { 
    $text: { 
      $search: ""avengers" "2012"" 
    }
  }
  const cursor = await mydb.find(query).project(projection);
}

Above code gives Perfect Search Result for me.
But I don’t know how to insert that word1 & word2 Parameters into the Function as Vars. If I replace avengers and 2012 words with parameters it read as constant.
How can I insert them to function as Variables…

2

Answers


  1. You need to use Javascript Template string

    $search: `"${word1}" "${word2}"`
    
    Login or Signup to reply.
  2. you could use

    const query = {
        $text: {
            $search: `"${word1}" "${word2}"`
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search