【数据库学习】java使用Mongodb增删改查

本文介绍如何使用Java连接MongoDB数据库,并演示了增删改查等基本操作。通过实例代码展示了如何进行数据的插入、查询、更新及删除。



一,下载驱动

玩数据库,木有驱动,那怎么行,驱动下载链接



二,java 连接数据库

就一句代码:

  // 连接到Mongodb服务
        MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);



三。增删改查,mongodb

(1). 增:

 List<Document> list = new ArrayList<Document>();
            Document document1 = new Document();
            document1.put("type", 1);
            document1.put("name", "xiaoming");
            document1.put("age", 15);
            document1.put("updatetime", insertDay);
            Document document2 = new Document();
            document2.put("type", 2);
            document2.put("name","小北");
            list.add(document2);
            // 单条插入
            collection.insertOne(document1);
            // 多条插入
            collection.insertMany(list);

(2). 删除:

 // 批量删除。热度小于10的全部删除掉
            collection.deleteMany(Filters.lte("hotness", 10));

(3). 改:

 // 批量更新。所有包含hotness属性的其hotness值乘以2
            collection.updateMany(Filters.exists("hotness"), new Document(
                    "$mul", new Document("hotness", 2)));

(4). 查

 // 全表遍历
            cursor = collection.find().iterator();
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }



三。全部代码如下:(驱动必须加的哦

package xatu.zsl.mongodb;

/**
 * Created by zsl on 2017/8/8.
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongoDBJDBC {

    public static void main(String args[]) {
        Calendar today = Calendar.getInstance();
        Date endDay = today.getTime();
        today.add(Calendar.DAY_OF_YEAR, -6);
        Date insertDay = today.getTime();
        today.add(Calendar.MONTH, -6);
        Date beginDay = today.getTime();
        System.out.println(beginDay);
        System.out.println(insertDay);
        System.out.println(endDay);
        // 连接到Mongodb服务
        MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
        try {
            // 连接到数据库.如果库不存在则创建
            MongoDatabase db = mongoClient.getDatabase("demo");
            // 选择表.如果表不存在则创建表
            MongoCollection<Document> collection = db
                    .getCollection("suggestion");
            // 清空表
            collection.deleteMany(Filters.exists("_id"));
            // 创建索引.复合索引,按type升序,按cont降序
            collection.createIndex(new Document("type", 1).append("cont", -1));
            // 构造Document
            List<Document> list = new ArrayList<Document>();
            Document document1 = new Document();
            document1.put("type", 1);
            document1.put("name", "xiaoming");
            document1.put("age", 15);
            document1.put("updatetime", insertDay);
            Document document2 = new Document();
            document2.put("type", 2);
            document2.put("name","小北");
            list.add(document2);
            // 单条插入
            collection.insertOne(document1);
            // 多条插入
            collection.insertMany(list);
            // 按条件查询遍历表
            MongoCursor<Document> cursor = collection.find(
                    Filters.and(Filters.gte("updatetime", beginDay),
                            Filters.lt("updatetime", endDay))).iterator();
            while (cursor.hasNext()) {
                Document document = cursor.next();
                if (document.containsKey("companyid")) {
                    document.put("hotness", document.getDouble("hotness") + 10);
                    // 更新。若包含companyid属性,则热度加10后更新
                    collection.updateOne(
                            Filters.eq("_id", document.get("_id")),
                            new Document("$inc", new Document("hotness", 10)));
                }
            }
            // 批量更新。所有包含hotness属性的其hotness值乘以2
            collection.updateMany(Filters.exists("hotness"), new Document(
                    "$mul", new Document("hotness", 2)));
            // 批量删除。热度小于10的全部删除掉
            collection.deleteMany(Filters.lte("hotness", 10));
            // 全表遍历
            cursor = collection.find().iterator();
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            mongoClient.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鼠晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值