我正在尝试构建一个 mongoose 搜索查询,用于搜索以搜索词开头和结尾的不同字段,但结果仍然是 []。
data.csv
bookID,title,authors,average_rating,isbn,isbn13,language_code, num_pages,ratings_count,text_reviews_count,publication_date,publisher,
1,Harry Potter and the Half-Blood Prince (Harry Potter #6),J.K. Rowling/Mary GrandPré,4.57,439785960,9.78044E+12,eng,652,2095690,27591,9/16/06,Scholastic Inc.,
2,Harry Potter and the Order of the Phoenix (Harry Potter #5),J.K. Rowling/Mary GrandPré,4.49,439358078,9.78044E+12,eng,870,2153167,29221,9/1/04,Scholastic Inc.,
4,Harry Potter and the Chamber of Secrets (Harry Potter #2),J.K. Rowling,4.42,439554896,9.78044E+12,eng,352,6333,244,11/1/03,Scholastic,
5,Harry Potter and the Prisoner of Azkaban (Harry Potter #3),J.K. Rowling/Mary GrandPré,4.56,043965548X,9.78044E+12,eng,435,2339585,36325,5/1/04,Scholastic Inc.,
8,Harry Potter Boxed Set Books 1-5 (Harry Potter #1-5),J.K. Rowling/Mary GrandPré,4.78,439682584,9.78044E+12,eng,2690,41428,164,9/13/04,Scholastic,
9,"Unauthorized Harry Potter Book Seven News: ""Half-Blood Prince"" Analysis and Speculation",W. Frederick Zimmerman,3.74,976540606,9.78098E+12,en-US,152,19,1,4/26/05,Nimble Books,
10,Harry Potter Collection (Harry Potter #1-6),J.K. Rowling,4.73,439827604,9.78044E+12,eng,3342,28242,808,9/12/05,Scholastic,
12,The Ultimate Hitchhiker's Guide: Five Complete Novels and One Story (Hitchhiker's Guide to the Galaxy #1-5),Douglas Adams,4.38,517226952,9.78052E+12,eng,815,3628,254,11/1/05,Gramercy Books,
13,The Ultimate Hitchhiker's Guide to the Galaxy (Hitchhiker's Guide to the Galaxy #1-5),Douglas Adams,4.38,345453743,9.78035E+12,eng,815,249558,4080,4/30/02,Del Rey Books,
Book.find({$or: [
{ title: {$regex : "^" + req.params.search + "^"} },
{ authors: {$regex : "^" + req.params.search + "^"} },
{ publisher: {$regex : "^" + req.params.search + "^"} },
{ language_code: {$regex : "^" + req.params.search + "^"} }
]}, function(err, result) {
if (err) {
console.log('err', err)
res.send(err);
} else {
console.log('result', result)
res.send(result);
}
});
答案 0 :(得分:0)
您的正则表达式是错误的。您需要使用 $
来表示结束。
{ title: {$regex : "^" + req.params.search + "$"} },
...