Java操作MongoDB(进阶篇)

1.导包

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.0.4</version>
        </dependency>

2.操作

一些简单的增删改查我就不说了,百度一大堆,这里主要讲比较复杂的查询

我使用的可视化工具是: nosqlbooster4mongo-6.1.8;提取码:pxgz

分组查询

释义:

对user表中createTime小于1603814400000的数据,按照userName进行分组,排除出现次数少于两次的数据,显示userName和出现次数

MongoDB:

db.user.aggregate([
 	{
        $match : { 
             "createTime":{$lt : 1603814400000}
        }
    }{
        $group : {
            _id : "$userName",
            count: { $sum : 1}
        }
    }
    ,
    {
        $match : { 
            count : { $gt : 2 }
        }
    }
],{ allowDiskUse: true })

时间存储我建议直接用时间戳,用Date类型一大堆坑

allowDiskUse: true:这一段在数据量大的时候需要加上,不然会报错
$match:放前面相当于mysql里面的where,放后面相当于mysql里面的having

比较函数释义
$gt(>) 大于
$lt(<) 小于
$gte(>=) 大于等于
$lte(<= ) 小于等于
统计函数释义
$sum:“$字段“统计字段之和(换成1就是统计条数)
$avg:“$字段“统计平均值
$max:“$字段“统计最大值
$min:“$字段“统计最小值
$first:“$字段“显示第一条数据的值
$last:“$字段“显示最后一条数据的值
$multiply:[ “$字段1”, “$字段2” ]统计多个字段之和

Java:

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb");
        MongoCollection<Document> collection = mongoDatabase.getCollection("user");

        Document where = new Document("$match",new Document("createTime",new Document("$lt",1603814400000L)));

        Document group = new Document("$group", new Document("_id", "$userName").append("count",new Document("$sum",1)));

        Document match = new Document("$match",new Document("count",new Document("$gt",2)));

        List<Document> aggregateList = new ArrayList<>();
        aggregateList.add(where);
        aggregateList.add(group);
        aggregateList.add(match);

        MongoCursor<Document> cursor = collection.aggregate(aggregateList).allowDiskUse(true).iterator();

        List<Document> list = new ArrayList<>();
        while (cursor.hasNext()){
            Document next = cursor.next();
            System.out.println(next);
        }
        mongoClient.close();

MySql:

SELECT
	userName AS _id,
	sum( 1 ) AS count 
FROM
	`user` 
WHERE
	createTime < 1603814400000 GROUP BY userName HAVING count >2

多条件模糊查询

释义:

查询user表,address包含深圳或广州,并且userName以一结尾、phone以134开头的信息

MongoDB:

db.user.find(
    {
        $and:[
            {"userName":{$regex:"一$"}},
            {"phone":{$regex:"^134"}}
        ],
        $or:[
            {"address":{$regex:"广州"}},
            {"address":{$regex:"深圳"}}
        ]
    }
)

Java:

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb");
        MongoCollection<Document> collection = mongoDatabase.getCollection("user");

        Document userNameDocument = new Document("userName",new Document("$regex","一$"));
        Document phoneDocument = new Document("phone",new Document("$regex","^134"));

        List<Document> and = new ArrayList<>();
        and.add(userNameDocument);
        and.add(phoneDocument);

        Document address1 = new Document("userName",new Document("$regex","广州"));
        Document address2 = new Document("phone",new Document("$regex","深圳"));

        List<Document> or = new ArrayList<>();
        and.add(address1);
        and.add(address2);

        Document document = new Document();
        document.append("$and",and);
        document.append("$or",or);

        MongoCursor<Document> iterator = collection.find(document).iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

MySql:

SELECT
	* 
FROM
	`user` 
WHERE
	userName LIKE '%一' 
	AND phone LIKE '134%' 
	AND ( address LIKE '%广州%' OR address LIKE '%深圳%' )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值