I can use this code to get all the data from MongoDB.
import nextConnect from 'next-connect';
import middleware from '../database';
const handler = nextConnect();
handler.use(middleware);
handler.get(async (req, res) => {
let doc = [];
await req.db.collection("mountain").find().forEach(element => {
doc.push(element);
});
console.log(doc);
res.json(doc);
});
export default handler;
But I can't get the data with the condition.
import nextConnect from 'next-connect';
import middleware from '../database';
const handler = nextConnect();
handler.use(middleware);
handler.get(async (req, res) => {
let doc = await req.db.collection("mountain").find({ name: "Lion Rock" })
console.log(doc);
res.json(doc);
});
export default handler;
The data in my MongoDB is like this:
Does anyone know where is the problem? Thank you!