MongoDB 应用实战
    最新驱动的地址
    https://mongodb.github.io/mongo-java-driver/4.1/driver/
    1. java 原生客户端
    引入maven

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.mongodb</groupId>
    4. <artifactId>mongodb-driver-sync</artifactId>
    5. <version>4.1.1</version>
    6. </dependency>
    7. </dependencies>
    1. public class QuickStart {
    2. public static void main(String[] args) {
    3. // 连接本地默认端口的Mongod
    4. // MongoClient mongoClient = MongoClients.create()
    5. // 连接远程服务的指定端口的Mongod
    6. // MongoClient mongoClient = MongoClients.create("mongodb://host1:27017");
    7. // 连接指定端口复制集
    8. // MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet");
    9. // 连接 mongos路由: 连接一个
    10. // MongoClient mongoClient = MongoClients.create( "mongodb://localhost:27017" );
    11. // 连接多个mongos路由
    12. MongoClient mongoClient = MongoClients.create("mongodb://111.229.189.98:4000");
    13. //获取数据库
    14. MongoDatabase database = mongoClient.getDatabase("productdb");
    15. // 获取集合
    16. MongoCollection<Document> productdesc=database.getCollection( "productdesc" );
    17. Document doc = new Document("name", "MongoDB")
    18. .append("type", "database")
    19. .append("count", 1)
    20. .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
    21. .append("info", new Document("x", 203).append("y", 102));
    22. productdesc.insertOne(doc);
    23. Bson eq = eq("name", "MongoDB");
    24. FindIterable<Document> find = productdesc.find(eq);
    25. Document first=find.first();
    26. System.out.println(first);
    27. }
    28. }
    1. Spring Boot 整合:
      https://docs.spring.io/spring-data/mongodb/docs/3.1.2/reference/html/#preface
      1. 引入maven ```java <?xml version=”1.0” encoding=”UTF-8”?> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.0 com.example demo 0.0.1-SNAPSHOT demo

      Demo project for Spring Boot 1.8

      org.springframework.boot spring-boot-starter-data-mongodb org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.projectlombok lombok 1.18.12

    org.springframework.boot spring-boot-maven-plugin

    1. 2. 添加配置类<br />a.yml中配置:
    2. ```java
    3. 无密码:test为库名
    4. spring.data.mongodb.uri=mongodb://localhost:27017/test
    5. spring:
    6. data:
    7. mongodb:
    8. uri: mongodb://localhost:27017/test
    9. 有密码:root,123456,test为库名
    10. spring:
    11. data:
    12. mongodb:
    13. uri: mongodb://root:123456@localhost:27017/test

    b.代码中配置:

    1. package com.example.demo.config;
    2. import com.mongodb.client.MongoClient;
    3. import com.mongodb.client.MongoClients;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import org.springframework.data.mongodb.core.MongoTemplate;
    7. @Configuration
    8. public class AppConfig {
    9. public @Bean
    10. MongoClient mongoClient() {
    11. return MongoClients.create("mongodb://111.229.189.98:4000");
    12. }
    13. public @Bean
    14. MongoTemplate mongoTemplate() {
    15. return new MongoTemplate(mongoClient(), "productdb");
    16. }
    17. }
    1. 测试类 ```java package com.example.demo;

    public class Person {

    private String id; private String name; private int age;

    public Person(String name, int age) { this.name = name; this.age = age; }

    public String getId() { return id; } public String getName() { return name; } public int getAge() { return age; }

    @Override public String toString() { return “Person [id=” + id + “, name=” + name + “, age=” + age + “]”; }

    }

    package com.example.demo;

    import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Component;

    import java.util.List;

    import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.query; import static org.springframework.data.mongodb.core.query.Update.update;

    @Component @Slf4j public class ApplicationRunnerTest implements ApplicationRunner{

    1. @Autowired
    2. private MongoTemplate mongoOps;
    3. @Override
    4. public void run(ApplicationArguments applicationArguments) throws Exception {
    5. Person p = new Person("Joe", 34);
    6. // 插入文档
    7. mongoOps.insert(p);
    8. log.info("Insert: " + p);
    9. // 查询文档
    10. p = mongoOps.findById(p.getId(), Person.class);
    11. log.info("Found: " + p);
    12. // 更新文档
    13. mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
    14. p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
    15. log.info("Updated: " + p);
    16. // 删除文档
    17. mongoOps.remove(p);
    18. // Check that deletion worked
    19. List<Person> people = mongoOps.findAll(Person.class);
    20. log.info("Number of people = : " + people.size());
    21. mongoOps.dropCollection(Person.class);
    22. }

    } ```