Mongo $ in不区分大小写的查询 [英] Mongo $in query with case-insensitivity

查看:83
本文介绍了Mongo $ in不区分大小写的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongoose.js来执行$ in查询,就像这样:

I'm using Mongoose.js to perform an $in query, like so:

userModel.find({
            'twitter_username': {
                $in: friends
            }
        })

friends只是一个字符串数组.但是,我遇到一些大小写问题,想知道是否可以使用Mongo的$ regex功能使此$ in查询不区分大小写?

friends is just an array of strings. However, I'm having some case issues, and wondering if I can use Mongo's $regex functionality to make this $in query case-insensitive?

推荐答案

来自 docs :

要在$ in查询表达式中包含正则表达式,您可以 仅使用JavaScript正则表达式对象(即/pattern/).为了 例如:

To include a regular expression in an $in query expression, you can only use JavaScript regular expression objects (i.e. /pattern/ ). For example:

{名称:{$ in:[/^ acme/i,/^ ack/]}}

{ name: { $in: [ /^acme/i, /^ack/ ] } }

一种方法是为每个Match创建正则表达式并形成friends数组.

One way is to create regular Expression for each Match and form the friends array.

 var friends = [/^name1$/i,/^name2$/i];
 or,
 var friends = [/^(name1|name2)$/i]
 userModel.find({"twitter_username":{$in:friends }})

这篇关于Mongo $ in不区分大小写的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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