MongoDB基础引用

1.引入MongoDB依赖:

<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.14</version>
</dependency>

2.连接MongoDB
方式一:直接进行连接:

public static void main(String[] args) {
    //使用ip和端口进行连接,MongoClient是MongoDB提供的java驱动类
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    //连接到数据库,其中personl为库名
    MongoDatabase database = mongoClient.getDatabase("personl");
    //获取连接的数据库中所有的表
    MongoIterable<String> listCollectionNames = database.listCollectionNames();
    for (String name : listCollectionNames) {
        System.out.println(name);
    }
    //连接指定的数据库,
    MongoCollection<Document> user = database.getCollection("指定的数据库名");
    //获得指定库中的数据
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
}

方式二:使用 MongoCredential

public static void main(String[] args) {

    //使用MongoCredential中的createCredential指定用户名,密码以及使用的数据库
    MongoCredential credential = MongoCredential.createCredential("user", "personl", "password".toCharArray());
    ServerAddress serverAddress = new ServerAddress("localhost", 27017);
    MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
    //连接到数据库,其中personl为库名
    MongoDatabase database = mongoClient.getDatabase("personl");
    //获取连接的数据库中所有的表
    MongoIterable<String> listCollectionNames = database.listCollectionNames();
    for (String name : listCollectionNames) {
        System.out.println(name);
    }
    //连接指定的数据库
    MongoCollection<Document> user = database.getCollection("users");
    //获得指定库中的数据
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
}

方式三:使用MongoClientURI

public static void main(String[] args) {
    String url = String.format("mongodb://127.0.0.1:27017", "user", "password", "localhost", 27017, "personl");
    MongoClientURI uri = new MongoClientURI(url);
    MongoClient mongoClient = new MongoClient(uri);
    //连接到数据库,其中personl为库名
    MongoDatabase database = mongoClient.getDatabase("personl");
    //获取连接的数据库中所有的表
    MongoIterable<String> listCollectionNames = database.listCollectionNames();
    for (String name : listCollectionNames) {
        System.out.println(name);
    }
    //连接指定的数据库
    MongoCollection<Document> user = database.getCollection("users");
    //获得指定库中的数据。find用来查找符合条件的文档
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
}

3.使用MongoDB进行数据的增删改查
1.新增数据(单条数据)使用:insertOne

@Test
public void test01() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("personl");
    MongoCollection<Document> user = database.getCollection("users");
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sdf.format(date);
    for (int i = 0; i < 5; i++) {
        Document doc = new Document();
        doc.put("name", "mongodbTestMany00"+i);
        doc.put("data", format);
        user.insertOne(doc);
    }
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
    //关闭链接
    mongoClient.close();
}

结果:
在这里插入图片描述
在这里插入图片描述

2.批量新增数据:

使用:insertMany

@Test
public void test02() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("personl");
    MongoCollection<Document> user = database.getCollection("users");
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sdf.format(date);
    List<Document> documentList = new ArrayList<>();
    for (int i = 5; i < 7; i++) {
        Document doc = new Document();
        doc.put("name", "mongodbTestMany00"+i);
        doc.put("data", format);
        documentList.add(doc);
    }
    user.insertMany(documentList);
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
    //关闭链接
    mongoClient.close();
}

3.更新
更新单条数据

@Test
public void test03() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("personl");
    MongoCollection<Document> user = database.getCollection("users");
    //更新单条数据
    user.updateMany(Filters.eq("name", "mongodbTestMany000"), Updates.set("name", "测试MongoDB"));
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
    //关闭链接
    mongoClient.close();
}
// 批量更新:使用bulkWrite
    @Test
    public void test04() {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("personl");
        MongoCollection<Document> user = database.getCollection("users");
        //更新单条数据
//        user.updateMany(Filters.eq("name", "mongodbTestMany000"), Updates.set("name", "测试MongoDB"));
        // 批量更新文档
        List<UpdateOneModel<Document>> updateModels = Arrays.asList(
                new UpdateOneModel<>(new Document("name", "mongodbTestMany000"), new Document("$set", new Document("name", "测试MongoDB"))),
                new UpdateOneModel<>(new Document("name", "mongodbTestMany001"), new Document("$set", new Document("name", "测试MongoDB")))
        );
        user.bulkWrite(updateModels);
        FindIterable<Document> documents = user.find();
        for (Document document : documents) {
            System.out.println(document);
        }
        //关闭链接
        mongoClient.close();
    }
删除:
@Test
public void test04() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("personl");
    MongoCollection<Document> user = database.getCollection("users");
    Bson gt = Filters.gt("name", "测试MongoDB");
    Document document = new Document();
    document.put("name","mongodbTestMany002");
    DeleteResult deleteResult = user.deleteOne(document);
    DeleteResult deleteResult1 = user.deleteMany(gt);
    //关闭链接
    mongoClient.close();
}
查询:
@Test
public void test05() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("personl");
    MongoCollection<Document> user = database.getCollection("users");
    FindIterable<Document> documents = user.find();
    for (Document document : documents) {
        System.out.println(document);
    }
    System.out.println("================================");
    Bson regex = Filters.regex("name", "mongodbTestMany003");
    FindIterable<Document> documents1 = user.find(regex);
    for (Document document : documents1) {
        System.out.println(document);
    }
    //关闭链接
    mongoClient.close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值