【问题标题】:How do I get Search data with express mongodb如何使用 express mongodb 获取搜索数据
【发布时间】:2020-12-28 15:52:30
【问题描述】:

这是我的 CRUD 代码,现在我想使用动态值执行搜索。

const express = require('express')
const router = express.Router()
const cors = require('cors')

//Importing Jobs Model
const JobsModel = require('../../models/jobs')


//POST Req 
router.post('/', async (req,res) => {
    const newJobPost = new JobsModel(req.body)
    try{
        const jobPost = await newJobPost.save()
        if(!jobPost) throw Error('Error, JobPost Not Saved...!')

        res.status(200).json(jobPost)
    }catch(err){
        res.status(400).json({msg:err})
    }
})

//GET All Req 
router.get('/',cors(), async (req,res) => {
    try{
        const getJobs = await JobsModel.find()
        if(!getJobs) throw Error('Error, No Jobs Found...!')
        res.status(200).json(getJobs)
    }catch(err){
        res.status(400).json({msg:err})
    }
})

//GET Single Req 
router.get('/:id',cors(), async (req,res) => {
    try{
        const getJob = await JobsModel.findById(req.params.id)
        if(!getJob) throw Error('Error, Job Not Found...!')
        res.status(200).json(getJob)
    }catch(err){
        res.status(400).json({msg:err})
    }
})


//DELETE Req 
router.delete('/:id',cors(), async (req,res) => {
    try{
        const delJobs = await JobsModel.findByIdAndDelete(req.params.id)
        if(!delJobs) throw Error('No Jobs Found to Delete...!')
        res.status(200).json({success: true})
    }catch(err){
        res.status(400).json({msg:err})
    }
})


//UPDATE Req 
router.patch('/:id',cors(), async (req,res) => {
    try{
        const updateJob = await JobsModel.findByIdAndUpdate(req.params.id, req.body)
        if(!updateJob) throw Error('Error, No Jobs Found to Update...!')
        res.status(200).json({success: true})
    }catch(err){
        res.status(400).json({msg:err})
    }
})

//SEARCH Req


module.exports = router

请那里的任何人帮助我执行搜索查询。我也在谷歌上做了很多研究,以找到“从 mongodb get req 搜索特定数据”的解决方案.......................... ..................................................... .......

【问题讨论】:

  • 请指定您要搜索的内容、所需的结果并提供您的数据库或模型的示例结构
  • const mongoose = require('mongoose') const Schema = mongoose.Schema const JobSchema = new Schema({ title:{ type: String, required: true }, category:{ type: String, required: true }, company:{ type: String, required: true }, city:{ type: String, required: true }, description:{ type: String, required: true } }) module.exports = mongoose.model('Jobs',JobSchema) 这是包含所有 mongodb 字段的模型文件
  • 您要搜索什么?按标题、城市或什么?
  • 我要按标题、类别和城市搜索
  • 我真的被困在这请帮助@Qudusayo

标签: javascript node.js mongodb express rest


【解决方案1】:

使用 mongoose 使用 find() 函数在 mongodb 中查找元素,该函数接受可选参数。
要按标题查找,您需要按返回回调函数的任何内容查找

JobsModel.find({title: req.body.title})

【讨论】:

  • 如何执行这样的查找` //GET All Req router.get('/',cors(), async (req,res) => { try{ const getJobs = await JobsModel. find() if(!getJobs) throw Error('Error, No Jobs Found...!') res.status(200).json(getJobs) }catch(err){ res.status(400).json({味精:错误}) } })`
  • 如您所见,我的 Get、Post、delete 和 Update 我该如何编写 SEARCH 那样的内容?
  • //SEARCH Req router.get('/:title',cors(), async (req,res) => { try{ const findJobs = await JobsModel.find({title: req.body.title}) if(!findJobs) throw Error('Error, Job Not Found...!') res.status(200).json(findJobs) }catch(err){ res.status(400).json({msg:err}) } }) 这也不行
  • 我使用类似的承诺: JobsModel.find({ title: req.body.title }).then((results) => { / * 你的过滤器放在这里* / }); then 函数中的结果是一个数组,您现在可以过滤掉结果,尝试记录您的结果以便快速解决。
猜你喜欢
  • 2021-02-06
  • 2021-03-12
  • 2020-03-30
  • 2018-09-03
  • 2016-01-03
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多