https://ip99202.github.io/posts/nodejs,-mongodb-게시판-검색-기능
models/products.js
historySchema.index({NIIN:"text", productName:"text"})
app.js
// 검색기능
app.post('/history/search',[query('content'), query('option')], function(req, res) {
var options = []
if (req.query.option == 'NIIN') {
options = [{ NIIN: new RegExp(req.query.content) }]
} else if (req.query.option == 'productName') {
options = [{ productName: new RegExp(req.query.content) }]
} else if (req.query.option == 'storageName') {
options = [{ storageName: new RegExp(req.query.content) }]
} else {
const err = new Error('검색 옵션이 없습니다.')
err.status = 400
throw err;
}
const history = await History.find({ $or: options })
res.json(history);
});