文档型数据库MongoDB-Java操作MongoDB

文档型数据库MongoDB-Java操作MongoDB

  • mongodb-driver
    • 查询全部记录
    • 条件查询
      • (1)查询userid为1013的记录
      • (2)查询浏览量大于1000的记录
      • 插入数据
    • SpringDataMongoDB

mongodb-driver

mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动。我们通过一个入门的案例来了解mongodb-driver的基本使用;

查询全部记录

(1)创建工程 mongoDemo, 引入依赖

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.6.3</version>
        </dependency>
    </dependencies>

(2)创建测试类

package com.tensquare;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDemo {

    public static void main(String[] args) {
        MongoClient client = new MongoClient("192.168.2.10");           // 创建连接
        MongoDatabase spitdb = client.getDatabase("spitdb");            // 打开数据库
        MongoCollection<Document> spit = spitdb.getCollection("spit");  // 获取集合
        FindIterable<Document> documents = spit.find();                 // 查询记录获取文档集合

        for(Document document : documents) {
            System.out.println("内容:" + document.getString("content"));
            System.out.println("用户ID:" + document.getString("userid"));
            System.out.println("浏览量:" + document.getInteger("visits"));
        }
        client.close();//关闭连接
    }
}

条件查询

BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。

(1)查询userid为1013的记录

package com.tensquare;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDemo1 {

    public static void main(String[] args) {
        MongoClient client = new MongoClient("192.168.2.10");           // 创建连接
        MongoDatabase spitdb = client.getDatabase("spitdb");            // 打开数据库
        MongoCollection<Document> spit = spitdb.getCollection("spit");  // 获取集合
        BasicDBObject bson = new BasicDBObject("userid","1013");        // 构建查询条件
        FindIterable<Document> documents = spit.find(bson);             // 查询记录获取结果集合

        for(Document document:documents) {
            System.out.println("内容:" + document.getString("content"));
            System.out.println("用户ID:" + document.getString("userid"));
            System.out.println("浏览量:" + document.getInteger("visits"));
        }

        client.close();//关闭连接
    }
}

(2)查询浏览量大于1000的记录

package com.tensquare;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDemo2 {

    public static void main(String[] args) {
        MongoClient client=new MongoClient("192.168.2.10");                             // 创建连接
        MongoDatabase spitdb = client.getDatabase("spitdb");                            // 打开数据库
        MongoCollection<Document> spit = spitdb.getCollection("spit");                  // 获取集合
        BasicDBObject bson=new BasicDBObject("visits",new BasicDBObject("$gt",1000) );  // 构建查询条件
       
        FindIterable<Document> documents = spit.find(bson);//查询记录获取结 果集合
        for(Document document:documents){
            System.out.println("内容:"+  document.getString("content"));
            System.out.println("用户ID:"+document.getString("userid"));
            System.out.println("浏览量:"+document.getInteger("visits"));
        }

        client.close();//关闭连接
    }
}

插入数据

package com.tensquare;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MongoDemo3 {

    public static void main(String[] args) {
        MongoClient client = new MongoClient("192.168.2.10");           // 创建连接
        MongoDatabase spitdb = client.getDatabase("spitdb");            // 打开数据库
        MongoCollection<Document> spit = spitdb.getCollection("spit");  // 获取集合

        Map<String, Object> map = new HashMap();
        map.put("content", "我要吐槽");
        map.put("userid", "9999");
        map.put("visits", 123);
        map.put("publishtime", new Date());

        Document document = new Document(map);
        spit.insertOne(document);               // 插入数据
        client.close();
    }
}

SpringDataMongoDB

SpringData家族成员之一,用于操作MongoDb的持久层框架,封装了底层的mongodb-driver。
官网主页: https://projects.spring.io/spring-data-mongodb/
当前项目的吐槽微服务就采用SpringDataMongoDB框架

你可能感兴趣的:(【微服务】,【MongoDB】,【项目】,【微服务项目】)