客户端模糊查询语句
db.user_base.find({ "umac" : { "$regex" : ".*Programming" , "$options" : "i"}} ).size()
Java模糊查询
/**
* 模糊查询
*
* @param db
* @param name
*/
public
static
void
query(DB db,String name){
DBCollection dbColl = db.getCollection(name);
//完全匹配
//Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE);
//右匹配
//Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE);
//左匹配
//Pattern pattern = Pattern.compile("^name.*$", Pattern.CASE_INSENSITIVE);
//模糊匹配
Pattern pattern = Pattern.compile(
"^.*name8.*$"
, Pattern.CASE_INSENSITIVE);
BasicDBObject query =
new
BasicDBObject();
query.put(
"name"
,pattern);
BasicDBObject sort =
new
BasicDBObject();
// 1,表示正序; -1,表示倒序
sort.put(
"name"
,
1
);
DBCursor cur = dbColl.find(query).sort(sort);
int
count =
0
;
while
(cur.hasNext()) {
DBObject obj = cur.next();
System.out.print(
"name="
+ obj.get(
"name"
));
System.out.print(
",email="
+ obj.get(
"email"
));
System.out.println(
",passwd="
+ obj.get(
"passwd"
));
count ++;
}
System.out.println(
"共有: "
+ count +
"个"
);
}