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

          MongoDB的模糊查詢

          Posted on 2012-06-27 15:44 yuhaibo736 閱讀(10450) 評論(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

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


          網站導航:
           
          主站蜘蛛池模板: 方正县| 老河口市| 凤凰县| 西盟| 石渠县| 桓台县| 双流县| 盘山县| 邻水| 潮州市| 吴忠市| 肥东县| 宁海县| 集贤县| 永康市| 平陆县| 海林市| 神池县| 礼泉县| 绥阳县| 镇雄县| 平顶山市| 从化市| 宁安市| 青川县| 莱西市| 尖扎县| 阳西县| 缙云县| 梧州市| 门源| 嘉祥县| 灵丘县| 大英县| 万全县| 广宗县| 洪洞县| 宾阳县| 梓潼县| 敦煌市| 五原县|