如何在mongodb中使用$ regex? [英] How to use $regex in mongodb?

查看:268
本文介绍了如何在mongodb中使用$ regex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据库中搜索以FOO开头或名称中包含FOO的客户.为此,我看了一个问题-> >如何使用"like"查询MongoDB; ?

I'd like to search my database for Customers that start with FOO or have FOO in their names. For that i took a look at this question -> How to query MongoDB with "like"?

在那之后,我建立了查询,使它看起来像这样->

After that i build my query so that it looks like this ->

Customer.find({'name': '/FOO/'}).exec(function (err, customer) {
     console.log(customer);
})

但是,尽管在'name'中有一个FOO的客户,但我没有得到任何结果.如果我将查询修改为...({'name': 'FOO'})...而不是...({'name': '/FOO/'})...,那么我会得到我的客户.我在做什么错了?

but although there is a customer with FOO inside the 'name' i get no result. If i modify my query to ...({'name': 'FOO'})... instead of ...({'name': '/FOO/'})... i get my customer. What am i doing wrong?

推荐答案

尝试使用 查看全文

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