【问题标题】:mongoose query $in with case insensitive regex not workingmongoose 查询 $in 不区分大小写的正则表达式不起作用
【发布时间】:2015-12-25 08:40:11
【问题描述】:

我在 mongoDB 中有一个电子邮件列,我必须检查数组中的字段,目前我正在使用以下查询

emailArray = [
  { '$regex': /test@email.com/i },
  { '$regex': /test2@email.com/i },
 ]
'$or': [ { email: emailArray }, { name: 'testUser' } ] }

我收到以下错误

路径“email”中值“[object Object]”的字符串转换失败

我不确定查询有什么问题:(

【问题讨论】:

    标签: regex mongodb mongoose mongodb-query


    【解决方案1】:

    你做错了。 emailArray 应该是正则表达式的数组,而且您在查询中缺少 $in

    var emailArray = [ /test@email.com/i, /test2@email.com/i ]
    db.collection.find({ 
        '$or': [ 
            { 'email': { '$in': emailArray } }, 
            { 'name': 'testUser' } 
        ] 
    })
    

    【讨论】:

    • 解决了我的问题,再次感谢您节省了我的时间 :)
    猜你喜欢
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    相关资源
    最近更新 更多