MongoDB的Java操作

// 需要三个jar包,mongodb-driver-3.3.0.jar;mongodb-drvier-core-3.3.0.jar;bson-3.3.0.jar
public class MongoTest {
    private MongoClient conn = null;

    // 获取连接
    private MongoClient getConn() {
        conn = new MongoClient("127.0.0.1", 27017);
        return conn;
    }

    // 关闭连接
    private void close(MongoClient conn) {
        conn.close();
    }

    public void insert() {
        conn = this.getConn();
        // 获取test数据库,没有自动创建
        MongoDatabase db = conn.getDatabase("test");
        // 获取ss集合,没有自动创建
        MongoCollection<Document> collection = db.getCollection("ss");

        // 创建文档
        Document doc = new Document("name", "lt").append("sex", "男").append("age", 30);
        // 单量插入
        collection.insertOne(doc);
        // 批量插入
        List<Document> docList = new ArrayList<Document>();
        for (int i = 0; i < 20; i++) {
            docList.add(new Document(..).append(..).append(..));
        }
        collection.insertMany(docList);

        this.close(conn);
    }

    public int delete() {
        conn = this.getConn();
        MongoDatabase db = conn.getDatabase("test");
        MongoCollection<Document> collection = db.getCollection("ss");

        // 单量删除
        collection.deleteOne(new Document("name", "new-lt10"));
        // 批量删除
        DeleteResult deleteResult = collection.deleteMany(..);

        this.close(conn);
        // 返回影响文档数
        return deleteResult.getDeletedCount();
    }

    public int update() {
        conn = this.getConn();
        MongoDatabase db = conn.getDatabase("test");
        MongoCollection<Document> collection = db.getCollection("ss");

        // 单量修改
        collection.updateOne(new Document("name", "lt10"), 
                new Document("$set", new Document("name", "new-lt10")));

        // 批量修改
        UpdateResult updateResult = collection.updateMany(new Document("name", "lt10"), 
                new Document("$set", new Document("name", "new-lt10")));

        this.close(conn);
        return updateResult.getModifiedCount()
    }

    public void list() {
        conn = this.getConn();
        MongoDatabase db = conn.getDatabase("test");
        MongoCollection<Document> collection = db.getCollection("ss");

        // 查询集合大小
        collection.count();

        // 查询集合的第一个文档
        Document myDoc = collection.find().first();
        myDoc.toJson();

        // 查询集合的全部文档
        MongoCursor<Document> cursor = collection.find().iterator();
        while (cursor.hasNext()) {
            cursor.next().toJson();
        }

        // 查询集合的全部文档
        for (Document cur : collection.find()) {
            cur.toJson();
        }

        // 按条件查询集合的指定文档
        MongoCursor<Document> cursor = collection.find(
                            new Document("name", "lt")).iterator();
        while (cursor.hasNext()) {
            cursor.next().toJson();
        }

        // 使用QueryBuilder按条件查询,找到name叫lt,且sex为man的记录
        QueryBuilder queryBuilder = 
            QueryBuilder.start("name").is("lt").and("set").is("man");


        this.close(conn);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值