java-mongodb查询笔记

本文记录了MongoDB的模糊查询、聚合查询、条件筛选等操作,包括使用$regex进行正则匹配,$ne判断非空,$where根据字符串长度筛选,以及$id的特殊查询。同时,文中提到了在Java中实现这些查询时遇到的问题,如$not的使用以及在Dubbo RPC调用中的序列化问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.模糊查询
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 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不能直接接$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); );





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值