doumeng1089 2016-12-01 06:01
浏览 172

MongoDB查找操作查询给出了错误的结果

This is my mongodb find query:

db.getCollection('candidate').find({
    "$or": [
      {
        "email_id": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      },
      {
        "mobile_no": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      },
      {
        "phone_no": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      },
      {
        "skill_name": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      },
      {
        "current_address": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      },
      {
        "first_name": {
          "$regex": {
            "regex": "^ranjit",
            "flags": "i"
          }
        }
      }
    ]
  }
)

Above mongo db query return me wrong tuples. It returns tuples who doesn't contain "ranjit", but actually I want tuples who contain "ranjit".

Could you explain why it's returning wrong tuples and what is the correct query?

  • 写回答

2条回答

  • drwo32555 2016-12-01 06:07
    关注

    Your can try this

      //like '%ranjit%'
      db.getCollection('candidate').find($OR:[...{""email_id": /ranjit/},....])
    
      //like 'ranjit%'
      db.getCollection('candidate').find($OR:[...{""email_id": /^ranjit/},....])
    
      //like '%ranjit'
      db.getCollection('candidate').find($OR:[...{""email_id": /ranjit$/},....])
    

    Using regex :

     db.getCollection('candidate').find($OR[... {"email_id" : {$regex : ".*ranjit.*"}},...]);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统