posts - 41, comments - 15, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          MongoDB的模糊查詢

          Posted on 2012-06-27 15:44 yuhaibo736 閱讀(10451) 評論(1)  編輯  收藏

          Mongo是沒有like查詢的方式的 
          要進行模糊匹配只能借助于正則表達式


          Java代碼:

               Mongo m=new Mongo();  

                DB db=m.getDB("UserDB");  

                DBCollection collection=db.getCollection("userCollection");  

                 

                BasicDBObject cond=new BasicDBObject();          


                if(province!=""){  

                   cond.put("province", province);

                }  

                if(area!=""){  

                   cond.put("area", area);  

                }              

                if(keywords!=""){  

                    Pattern pattern=Pattern.compile("^.*"+keywords+".*$");            

                    cond.put("name", pattern);

                }  

           

                DBCursor returns=collection.find(cond);  

           

          還有一種是mongoDB 和Spring結合的 Spring-data的模式查詢  代碼如下 :

                 public List<User> findUserTop9(String s) {  

                       mongoTemplate.setDatabaseName("UserDB");

           

                       mongoTemplate.setDefaultCollectionName("userColl");  

                       List<User> users = mongoTemplate.find(new Query(new Criteria(      "name").regex(".*?"+"張"+".*")).limit(9), User.class);  

                       return users;

                  }  


          評論

          # re: MongoDB的模糊查詢  回復  更多評論   

          2013-05-10 09:44 by asd
          wqeqwe

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 麟游县| 宜川县| 松溪县| 花垣县| 富锦市| 北辰区| 平塘县| 满城县| 南召县| 弥勒县| 明星| 苏尼特左旗| 湄潭县| 苗栗县| 鞍山市| 陵水| 类乌齐县| 泗阳县| 太仆寺旗| 阿巴嘎旗| 财经| 美姑县| 平原县| 葫芦岛市| 靖州| 东莞市| 松原市| 重庆市| 常熟市| 神木县| 辽阳市| 平南县| 吴堡县| 呼和浩特市| 凤庆县| 青海省| 沭阳县| 进贤县| 昌乐县| 莆田市| 吉安市|