Node.js 和 Mongoose 正则表达式查询多个字段 [英] Node.js and Mongoose regex query on multiple fields
本文介绍了Node.js 和 Mongoose 正则表达式查询多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用相同的正则表达式查询多个字段.在此查询中,我想接受单个文本输入并检查 firstName 和 lastName 字段以获取结果.我可以使用 mongoose 文档中的正则表达式函数很好地查询单个字段,但是or"子句的语法给我带来了麻烦.
I would like to query multiple fields using the same regular expression for both. In this query, I would like to accept a single text input and check both the firstName and lastName fields for results. I can query a single field just fine using the regex function in the mongoose documentation, but the syntax for an 'or' clause is giving me trouble.
var re = new RegExp(req.params.search, 'i');
app.User.find().or([{ 'firstName': { $regex: re }}, { 'lastName': { $regex: re }}]).sort('title', 1).exec(function(err, users) {
res.json(JSON.stringify(users));
});
(我在 node.js 0.6.12 上运行 mongoose 2.7.1)
(I'm running mongoose 2.7.1 on node.js 0.6.12)
推荐答案
以上代码适用于多个字段的查询.原来我有一些不好的数据.
The code above works fine for a query on multiple fields. Turns out I had some bad data.
这篇关于Node.js 和 Mongoose 正则表达式查询多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文