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.*"}},...]);
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码