您的位置:首页 > 数据库 > Mongodb

java-mongodb查询笔记

2018-02-23 11:26 344 查看
一.模糊查询1.只能是字符串2.代码如下:String tableName = "bidding_notice";String dbName = "spider";BasicDBObject qryParam = new BasicDBObject();qryParam.put(SpiderMapKeyConst.KEY_TASK_ID, "82");BasicDBObject regex = new BasicDBObject();regex.put("$regex", ".*?暂无数据.*?");qryParam.put(SpiderMapKeyConst.KEY_CONTENT, regex);long count = mongoService.count(dbName, tableName, qryParam);
关键在于正则regex.put("$regex", ".*?暂无数据.*?"
二.聚合查询格式{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }例子db.getCollection('bidding_notice').aggregate([ { "$match" :{ "siteId" :"346"}},{ "$group" : { "_id" : "$siteId" , "count" : { "$sum" : 1}}}])
三.$ne详解This will return all documents with a key called "IMAGE URL", but they may still have a null value.db.mycollection.find({"IMAGE URL":{$exists:true}});This will return all documents with both a key called "IMAGE URL" and a non-null value.db.mycollection.find({"IMAGE URL":{$ne:null}});Also, according to the docs, $exists currently can't use an index, but $ne can.Edit: Adding some examples due to interest in this answerGiven these inserts:db.test.insert({"num":1, "check":"check value"});db.test.insert({"num":2, "check":null});db.test.insert({"num":3});This will return all three documents:db.test.find();This will return the first and second documents only:db.test.find({"check":{$exists:true}});This will return the first document only:db.test.find({"check":{$ne:null}});This will return the second and third documents only:db.test.find({"check":null})三.根据字符串长度筛选
mongodb命令:db.getCollection('bidding_notice').count({"taskId":"82","content": {$ne: null}, $where:"this.content.length< 400"})
java代码:BasicDBObject qryParam = new BasicDBObject();BasicDBObject content = new BasicDBObject();content.put("$ne", null);qryParam.put(SpiderMapKeyConst.KEY_CONTENT, content);qryParam.put("$where", "this.content.length < 400");
四.根据id查询自定义id的时候,并不符合new OjbectId()的规定,所以要根据id查询的时候需要迂回一下
java代码:BasicDBObject query = new BasicDBObject();query.put("_id", id);
五.$not在Java代码的实现$ne不起作用的时候,就需要用$not,但是$not不能直接接$regex
mongodb查询代码:db.getCollection('bidding_notice').count({ "siteId" : "97", "parsed":4, "title" : { $not : /^.*?废标|失败|终止|中止|作废公告|示$/}})
我用的是3.4.2版本的mongo-driver+dubbo,消费者调用的时候有个坑,dubbo远程调用,参数一定要实现serialize接口,而mongoclient的filters是没有实现的,bson又是一个接口,还有java的pattern也无法通过rpc的decode
正常调用(本地):Pattern pattern = Pattern.compile("^.*?废标|失败|终止|中止|作废公告|示$");Bson bson = new BasicDBObject("title", pattern);Bson title = Filters.not(bson);// Bson title = new BasicDBObject(QueryOperators.NOT, bson); Bson and = Filters.and(title, new BasicDBObject("siteId", "97"), new BasicDBObject("parsed", 4));
mongoService.count(mongoService.getDefaultDbName(), tableName, and);
远程调用(RPC):Map<String, Object> query = Maps.newHashMap();Map<String, Object> title = Maps.newHashMap();// 注意这里与上面的区别,上面的可以直接用pattern,这里decode有问题title.put("title", "^.*?废标|失败|终止|中止|作废|流标公告|示$");query.put("$not", title);
List<Bson> queryList = Lists.newArrayList();Map patternMap = (Map) value;Set set = patternMap.keySet();for (Object key : set) {    String regexStr = MapUtils.getString(patternMap, key);    Pattern pattern = Pattern.compile(regexStr);    Bson bson = new BasicDBObject((String) key, pattern);    queryList.add(Filters.not(bson));}mongoService.count(mongoService.getDefaultDbName(), tableName, Filters.and(queryList););
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: