1. 程式人生 > >java-mongodb查詢筆記

java-mongodb查詢筆記

一.模糊查詢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"anda 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 answer
Given 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不能直接接$regexmongodb查詢程式碼: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);    Bson bson = new BasicDBObject((String) key, pattern);    queryList.add(Filters.not(bson));}mongoService.count(mongoService.getDefaultDbName(), tableName, Filters.and(queryList););