javascript - 如何在猫鼬中找到“按名称”
问题描述
我正在尝试使用特定名称获取数据库值。我想得到一个作者写的所有书。
这是我的代码,用于获取作者的名字并使用运算符搜索他在BookDB中编写的所有书籍,如Like
mysql
//get books by author name
router.route('/authorName').get((req, res) => {
let authorName = req.params.authorName; //how can i pass this value to below regex command
BookDB.find({"firstName": /authorName/}, (err, books) => {
if(err) throw err;
res.status(200).send(books);
});
});
问题:如何传递authorName
给 {"firstName": /authorName/}。我写的方式,设置不authorName
正确。
解决方案
我做了一些研究,发现$regex
。
//get books by author name
router.route('/authorName').get((req, res) => {
let authorName = req.params.authorName;
BookDB.find({firstName: {$regex: `${authorName}`}}, (err, books) => {
if(err) throw err;
res.status(200).send(books);
});
});
authorName
这将在数据BookDB
库中找到包含此特定字符串的所有结果
推荐阅读
- grafana - InfluxQL 数据源连接错误“xxxxxx....” 找不到数据库
- regex - 如何用数据获取数据
- java - 使用 Java 流解析 JsonArray 结果
- python - 使用 setup.py 和 requirements.txt 用 github 版本覆盖包?
- javascript - if else 语句在页面滚动事件中都一个接一个地执行
- angular - 当您进行返回可观察的 HTTP 调用时,我应该如何在 Angular 单元测试中使用 fakesAync?
- python - 特征值问题和 HPC 的矩阵变换
- ansible - Ansible 访问嵌套变量
- firebase - 我可以重复使用已删除项目中的 URL 吗?
- reactjs - 使用 axios 时未定义操作负载