天天看點

MongoDB的模糊查詢

MongoDB的模糊查詢  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");