I am currently working on developing an API that can return search results based on multiple parameters. So far, I have been able to successfully query one parameter.
For example, here is a sample URL:
http://localhost:3000/api/search?term=javascript&impact=sdg1
My goal is to get search results that meet both the conditions of term=javascript AND impact=sdg1
import Cors from "cors";
import initMiddleware from "../../util/init-middleware";
import { connectToDatabase } from "../../util/mongodb";
const cors = initMiddleware(
Cors({
methods: ["GET", "POST", "OPTIONS"],
})
);
export default async function handler(req, res) {
await cors(req, res);
const { db } = await connectToDatabase();
const data = await db
.collection("jobs")
.aggregate([
{
$search: {
search: [
{
query: req.query.term,
path: ["title", "role"],
},
{
query: req.query.impact,
path: ["impact"],
},
],
},
},
])
.sort({ date: -1 })
.toArray();
res.json(data);
}
I would like to know if it's feasible to achieve this and if anyone could recommend the appropriate type of query to use?
Thank you in advance