Node.js和Mongoose正则表达式查询多个字段 [英] Node.js and Mongoose regex query on multiple fields

查看:326
本文介绍了Node.js和Mongoose正则表达式查询多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的正则表达式查询两个字段。在这个查询中,我想接受一个文本输入,并检查结果的firstName和lastName字段。我可以使用mongoose文档中的正则表达式函数查询单个字段,但是'或'子句的语法给我的麻烦。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆