我正在尝试使用特定名称获取数据库值。我想得到一个作者写的所有书。
这是我的代码,用于获取作者的名字,并使用 Like
运算符搜索他在 BookDB 中编写的所有书籍在 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
。
最佳答案
我不确定 Mongoose 部分,但您也可以创建正则表达式: new RegExp(exp: string, flags: string)
请注意,如果您使用这种格式,开始和结束斜线没有特殊含义,它们表示字符串以斜线开头
关于javascript - 如何在 Mongoose 中找到 "By Name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56409831/