查询某个字段是否存在 :$exists
mongoDB:
db.getCollection('表名').find({ "字段名" : { "$exists" : true}})
true包含,false不包含.
对应的java代码:
DBObject query = new BasicDBObject();
BasicDBList vals = new BasicDBList();
vals.add(new BasicDBObject("字段名", new BasicDBObject("$exists",true/false)));
query.put("referenceUrl", new BasicDBObject("$exists", true/false));
包含:$in
mongoDB:
db.getCollection('表名').find({字段名:{$in:[0,1]}})
查询某个字段再0-1范围的数据,包含0和1
不包含: not-in
用法和上面的in一样 符号为$nin
模糊查询
mongoDB:
db.getCollection('表名').find({"字段名" : { "$regex" : "^.*模糊搜索的内容.*$"}})
这是完全模糊搜索,还包含从左模糊,从右模糊,中间模糊.比较啰嗦,就不用了,就用完全模糊就够用了
java:
Pattern pattern;
DBObject query = new BasicDBObject();
pattern = Pattern.compile("^.*" + fuzzyName + ".*$", Pattern.CASE_INSENSITIVE);
query.put("questionPaperName", pattern);