I'm trying to create an api which will return search results which match more than one parameter. I can query one parameter fine at the moment.
Here is an example url
http://localhost:3000/api/search?term=javascript&impact=sdg1
I want the results to include both 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);
}
Is this possible and can anyone suggest the right sort of query to write?
Many thanks in advance