【问题标题】:how to query using a specific field instead of id in express js如何在express js中使用特定字段而不是id进行查询
【发布时间】:2019-04-17 15:44:24
【问题描述】:

以下是我按城市查询搜索的代码。在我的帖子中找不到我> 我需要能够搜索特定城市并获取与该城市匹配的字段的所有记录。例如,如果 city 是 Downtown,我想要所有包含 city Downtown 的记录

            router.get("/search", (req, res, next) =>{
            const city = req.query.city;
            Facility.findAll(city)
                .select('name type mobile price streetName city state _id')
                .exec()
                .then(docs => {
                console.log("From database", docs);
                if (docs) {
                    res.status(200).json({
                        facility: docs
                    });
                } else {
                    res
                    .status(404)
                    .json({ message: "No valid entry found for provided City" });
                }
                })
                .catch(err => {
                console.log(err);
                res.status(500).json({ error: err });
                });

            });

【问题讨论】:

  • 您是否需要将_id 添加到选择提要中?只是好奇???
  • 请说明您遇到的错误
  • 消息:“模型“设施”的路径“_id”处的值“搜索”转换为 ObjectId 失败,

标签: node.js mongodb api express


【解决方案1】:

这是一个更好的方法;

router.get('/search', (req, res) => {
  const {city} = req.query
  Facility.find({city})
    .select('name type mobile price streetName city state')
    .exec((err, doc) => {
      if (err) {
        return res.status(500)
          .json({ message: 'error querying cities', error: err });
      }
      if (!docs) {
        return res.status(404)
          .json({ message: 'No valid entry found for provided City' });
      }
      return res.status(200)
        .json({
          facility: docs
        });
    })
})

find() 将完成这项工作。我认为您不需要选择_id,如果您不想要它,您只需指定-_id,但默认选择_id

以下是 Mongoose 中的有效查询列表(如果您正在使用该查询)

Model.deleteMany()
Model.deleteOne()
Model.find()
Model.findById()
Model.findByIdAndDelete()
Model.findByIdAndRemove()
Model.findByIdAndUpdate()
Model.findOne()
Model.findOneAndDelete()
Model.findOneAndRemove()
Model.findOneAndUpdate()
Model.replaceOne()
Model.updateMany()
Model.updateOne()

查看https://mongoosejs.com/docs/queries.html了解更多关于查询的信息

希望对你有帮助

【讨论】:

    【解决方案2】:

    你快到了。只需指定您的查询,就可以了

    https://mongoosejs.com/docs/api.html#model_Model.find

    router.get("/search", (req, res, next) =>{
            const city = req.query.city;
            Facility.findAll({propertyName: city})
                .select('name type mobile price streetName city state _id')
                .exec()
                .then(docs => {
                console.log("From database", docs);
                if (docs) {
                    res.status(200).json({
                        facility: docs
                    });
                } else {
                    res
                    .status(404)
                    .json({ message: "No valid entry found for provided City" });
                }
                })
                .catch(err => {
                console.log(err);
                res.status(500).json({ error: err });
                });
    
            });
    

    【讨论】:

    • 当我使用邮递员时它不起作用。请参阅下面的错误{“错误”:{“消息”:“模型\“设施\”的路径\“_id\”中的值\“搜索\”转换为ObjectId失败,“名称”:“CastError”,“ stringValue": "\"search\"", "kind": "ObjectId", "value": "search", "path": "_id" } } 下面是我使用的链接:localhost:9944/facilities/search?Sabo
    • 它仍在尝试查询 _id 属性。你能更新你的代码吗?
    【解决方案3】:

    下面是工作代码:

    router.get('/search', (req, res) => {
    const city = req.query.city
        Facility.find({city})
            .select('name type mobile price streetName city state')
                .exec((err, docs) => {
                    if (err) {
                        return res.status(500)
                        .json({ message: 'error querying cities', error: err });
                    }
                    if (!docs) {
                        return res.status(404)
                        .json({ message: 'No valid entry found for provided City' });
                    }
                    return res.status(200)
                        .json({
                        count: docs.length,
                        facility: docs
                    });
                })
    

    });

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多