SpringBoot 整合 MongoDB 实战解说

在前面的文章中,我们详细的介绍了 MongoDB 的配置和使用,如果你对 MongoDB 还不是很了解,也没关系,在 MongoDB 中有三个比较重要的名词: 数据库、集合、文档

  • 数据库(Database):和关系型数据库一样,每个数据库中有自己的用户权限,不同的项目组可以使用不同的数据库
  • 集合(Collection): 集合指的是文档组(类似于 Mysql 中的表的概念),里面可以存储许多文档
  • 文档(Document): 文档是 MongoDB 中最基本的数据单元,由键值对组成,类似于 JSON 格式,可以存储不同字段,字段的值可以包括其他文档、数组和文档数组

搞懂这三个名词,基本就可以上手了,今天我们以 Java 语言为例,详细的介绍 Java 客户端改如何操作 MongoDB !

话不多说,直接开撸!

Java 连接 MongoDB 服务器,与我们常用的连接关系型数据库方式类似!

标准 URI 连接语法:

mongodb://username:password@host1:port1,host2:port2,...,hostN:portN/database?options

参数说明:

  • mongodb://:这是固定的格式,必须要指定
  • username:password@:可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登录这个数据库
  • host1:port1:主机IP和端口号,必须指定至少一个host。如果要连接复制集,请指定多个主机地址
  • /database:如果指定了 username:password@,连接时会验证并登录指定数据库。若不指定,默认打开 test 数据库
  • ?options:连接可选项,例如 connectTimeoutMS=5000ms,表示连接等待时间最长 5 秒

例如,无密码连接 MongoDB

mongodb://127.0.0.1:27017

使用用户名 test,密码 test登录 MongoDB 的 test_db数据库

mongodb://test:test@127.0.0.1:27017/test_db

无密码连接指定三台服务器 (端口 27017, 27018, 和27019)

mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019

和关系型数据库操作一样,在 Java 程序中如果要使用 MongoDB,先要添加对应的 MongoDB JDBC 驱动器,然后才能继续操作!


    org.mongodb
    mongo-java-driver
    3.12.7

连接数据库的 Java 代码如下:

private static MongoClient mongoClient;

static {
    try {
        //连接到MongoDB服务 如果是远程连接可以替换"localhost"为服务器所在IP地址
        mongoClient = MongoClients.create("mongodb://test:test@127.0.0.1:27017/test_db");
        System.out.println("Connect to database successfully");
    } catch (Exception e) {
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    }
}

上文中采用的是用户名和密码方式登录数据库,因为数据库已经开启了权限验证,因此需要通过用户名和密码才能登录。

com.mongodb.client包里面都帮我们集成好了大部分常用的 api,我们可以通过 MongoDatabase类中的 createCollection()方法来创建集合。

创建一个 tb_role集合,代码片段如下:

/**
 * 创建集合
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    mongoDatabase.createCollection("tb_role");
    System.out.println("集合创建成功");
}

如果想删除集合,操作也很简单!

/**
 * 删除集合
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    mongoDatabase.getCollection("tb_role").drop();
    System.out.println("集合 tb_role 删除成功");
}

我们可以通过 MongoCollection类中的 insertMany()方法来插入一个文档。

tb_role集合中插入文档,代码片段如下:

/**
 * 插入文档
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    //获取集合
    MongoCollection collection = mongoDatabase.getCollection("tb_role");

    //向集合中插入文档
    Document document = new Document("name", "管理员").

            append("code", "manager").

            append("sort", 100);
    List documents = new ArrayList<>();
    documents.add(document);
    collection.insertMany(documents);
    System.out.println("文档插入成功");
}

我们可以通过 MongoCollection类中的 updateMany()方法来更新集合中的文档。

更新 tb_role集合中的指定文档信息,代码片段如下:

/**
 * 更新文档
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    //获取集合
    MongoCollection collection = mongoDatabase.getCollection("tb_role");

    //更新文档
    collection.updateMany(Filters.eq("code", "manager"), new Document("$set",new Document("name","经理")));

    //遍历所有文档
    FindIterable findIterable = collection.find();
    MongoCursor mongoCursor = findIterable.iterator();
    while(mongoCursor.hasNext()){
        System.out.println(mongoCursor.next());
    }
}

上文操作类似于 SQL语句中的 update tb_role set name = '&#x7ECF;&#x7406;' where code = 'manager'

我们可以通过 MongoCollection类中的 deleteOne()或者 deleteMany方法来删除集合中的文档。

删除 tb_role集合中的指定文档信息,代码片段如下:

/**
 * 删除文档
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    //获取集合
    MongoCollection collection = mongoDatabase.getCollection("tb_role");

    //删除符合条件的第一个文档
    collection.deleteOne(Filters.eq("code", "manager"));
    //删除所有符合条件的文档
    collection.deleteMany (Filters.eq("code", "manager"));

    //遍历所有文档
    FindIterable findIterable = collection.find();
    MongoCursor mongoCursor = findIterable.iterator();
    while(mongoCursor.hasNext()){
        System.out.println(mongoCursor.next());
    }
}

在这里需要注意一下,在删除的时候,官方推荐尽可能先查询一下这个数据是否存在,如果存在,然后在执行删除操作,可以调用 findOneAndDelete()方法进行删除!

MongoCollection类中提供了非常丰富的检索文档的方法,例如,我们可以通过 find()方法来获取集合中的所有文档。

检索 tb_role集合中的文档信息,代码片段如下:

/**
 * 查询文档
 * @param args
 */
public static void main(String[] args) {
    // 连接到数据库
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test_db");
    System.out.println("Connect to database successfully");
    //获取集合
    MongoCollection collection = mongoDatabase.getCollection("tb_role");

    //遍历所有文档
    FindIterable findIterable = collection.find();
    MongoCursor mongoCursor = findIterable.iterator();
    while(mongoCursor.hasNext()){
        System.out.println(mongoCursor.next());
    }

    //查询当前集合所有文档数量
    long count = collection.countDocuments();
    System.out.println("当前文档数量:" + count);

    //带条件遍历文档
    FindIterable documentFindIterable = collection.find(Filters.eq("code", "manager"));
    MongoCursor documentMongoCursor = documentFindIterable.iterator();
    while(documentMongoCursor.hasNext()){
        System.out.println(documentMongoCursor.next());
    }
}

采用 SpringBoot 来对接 MongoDB,可以说极其简单,下面就以常用的几个操作为例来介绍,过程如下!

<!-- 引入springboot -->
<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.1.0.RELEASE</version>
</parent>

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-data-mongodb</artifactid>
</dependency>

application.properties文件中添加 mongodb配置

#&#x914D;&#x7F6E;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x5730;&#x5740;
spring.data.mongodb.uri=mongodb://test123:test123@127.0.0.1:27017/test_db

创建用于示例中测试的实体类 Person

其中注解 @Document(collection="persons")用于指定当前文档属于集合 persons

注解 @Id表示当前 id字段属于主键类型。

/**
 * 使用@Document注解指定集合名称
 */
@Document(collection="persons")
public class Person implements Serializable {
    private static final long serialVersionUID = -3258839839160856613L;

    /**
     * 使用@Id注解指定MongoDB中的 _id 主键
     */
    @Id
    private Long id;

    private String userName;

    private String passWord;

    private Integer age;

    private Date createTime;

    //...get/set

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", age=" + age +
                ", createTime=" + createTime +
                '}';
    }
}

Springboot 操作 MongoDB 有两种方式。

  • 第一种方式是采用 Springboot 官方推荐的 JPA 方式,这种操作方式,使用简单但是灵活性比较差。
  • 第二种方式是采用 Spring Data MongoDB 封装的 MongoDB 官方 Java 驱动 MongoTemplate 对 MongoDB 进行操作,这种方式非常灵活,能满足绝大部分需求。

本文将采用第二种方式进行介绍!

MongoTemplate提供了 insert()方法,用于插入文档,示例代码如下:

  • 用于插入文档

没指定集合名称时,会取 @Document注解中的集合名称

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 插入文档
     * @throws Exception
     */
    @Test
    public void insert() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());
        mongoTemplate.insert(person);
    }
}
  • 自定义集合名称,插入文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 自定义集合,插入文档
     * @throws Exception
     */
    @Test
    public void insertCustomCollection() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());
        mongoTemplate.insert(person, "custom_person");
    }
}
  • 自定义集合,批量插入文档

如果采用批量插入文档,必须指定集合名称

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 自定义集合,批量插入文档
     * @throws Exception
     */
    @Test
    public void insertBatch() throws Exception {
        List personList = new ArrayList<>();
        Person person1 =new Person();
        person1.setId(10l);
        person1.setUserName("张三");
        person1.setPassWord("123456");
        person1.setCreateTime(new Date());
        personList.add(person1);

        Person person2 =new Person();
        person2.setId(11l);
        person2.setUserName("李四");
        person2.setPassWord("123456");
        person2.setCreateTime(new Date());
        personList.add(person2);

        mongoTemplate.insert(personList, "custom_person");
    }
}

MongoTemplate提供了 save()方法,用于存储文档。

在存储文档的时候会通过主键ID进行判断,如果存在就更新,否则就插入,示例代码如下:

  • 存储文档,如果没有插入,否则通过 &#x4E3B;&#x952E;ID更新
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 存储文档,如果没有插入,否则更新
     * @throws Exception
     */
    @Test
    public void save() throws Exception {
        Person person =new Person();
        person.setId(13l);
        person.setUserName("八八");
        person.setPassWord("123456");
        person.setAge(40);
        person.setCreateTime(new Date());
        mongoTemplate.save(person);
    }
}
  • 自定义集合,存储文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 自定义集合,存储文档
     * @throws Exception
     */
    @Test
    public void saveCustomCollection() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());
        mongoTemplate.save(person, "custom_person");
    }
}

MongoTemplate提供了 updateFirst()updateMulti()方法,用于更新文档,示例代码如下:

  • 更新文档,匹配查询到的文档数据中的第一条数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新文档,匹配查询到的文档数据中的第一条数据
     * @throws Exception
     */
    @Test
    public void updateFirst() throws Exception {
        //更新对象
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三123");
        person.setPassWord("123456");
        person.setCreateTime(new Date());

        //更新条件
        Query query= new Query(Criteria.where("id").is(person.getId()));

        //更新值
        Update update= new Update().set("userName", person.getUserName()).set("passWord", person.getPassWord());
        //更新查询满足条件的文档数据(第一条)
        UpdateResult result =mongoTemplate.updateFirst(query,update, Person.class);
        if(result!=null){
            System.out.println("更新条数:" + result.getMatchedCount());
        }
    }
}
  • 更新文档,匹配查询到的文档数据中的所有数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新文档,匹配查询到的文档数据中的所有数据
     * @throws Exception
     */
    @Test
    public void updateMany() throws Exception {
        //更新对象
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());

        //更新条件
        Query query= new Query(Criteria.where("id").is(person.getId()));

        //更新值
        Update update= new Update().set("userName", person.getUserName()).set("passWord", person.getPassWord());
        //更新查询满足条件的文档数据(全部)
        UpdateResult result = mongoTemplate.updateMulti(query, update, Person.class);
        if(result!=null){
            System.out.println("更新条数:" + result.getMatchedCount());
        }
    }
}

MongoTemplate提供了 remove()findAndRemove()findAllAndRemove()方法,用于删除文档,示例代码如下:

  • 删除符合条件的所有文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 删除符合条件的所有文档
     * @throws Exception
     */
    @Test
    public void remove() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());

        Query query = new Query(Criteria.where("userName").is(person.getUserName()));
        DeleteResult result = mongoTemplate.remove(query, Person.class);
        System.out.println("删除条数:" + result.getDeletedCount());
    }
}
  • 删除符合条件的单个文档,并返回删除的文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 删除符合条件的单个文档,并返回删除的文档
     * @throws Exception
     */
    @Test
    public void findAndRemove() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());

        Query query = new Query(Criteria.where("id").is(person.getId()));
        Person result = mongoTemplate.findAndRemove(query, Person.class);
        System.out.println("删除的文档数据:" + result.toString());
    }
}
  • 删除符合条件的所有文档,并返回删除的文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 删除符合条件的所有文档,并返回删除的文档
     * @throws Exception
     */
    @Test
    public void findAllAndRemove() throws Exception {
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());

        Query query = new Query(Criteria.where("id").is(person.getId()));
        List result = mongoTemplate.findAllAndRemove(query, Person.class);
        System.out.println("删除的文档数据:" + result.toString());
    }
}

MongoTemplate提供了非常多的文档查询方法,日常开发中用的最多的就是 find()方法,示例代码如下:

  • 查询集合中的全部文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询集合中的全部文档数据
     * @throws Exception
     */
    @Test
    public void findAll() throws Exception {
        List result = mongoTemplate.findAll(Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 查询集合中指定的ID文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询集合中指定的ID文档数据
     * @throws Exception
     */
    @Test
    public void findById() {
        long id = 1l;
        Person result = mongoTemplate.findById(id, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据条件查询集合中符合条件的文档,返回第一条数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据条件查询集合中符合条件的文档,返回第一条数据
     */
    @Test
    public void findOne() {
        String userName = "张三";
        Query query = new Query(Criteria.where("userName").is(userName));
        Person result = mongoTemplate.findOne(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据条件查询集合中符合条件的文档
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据条件查询集合中符合条件的文档
     */
    @Test
    public void findByCondition() {
        String userName = "张三";
        Query query = new Query(Criteria.where("userName").is(userName));
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据【AND】关联多个查询条件,查询集合中的文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据【AND】关联多个查询条件,查询集合中的文档数据
     */
    @Test
    public void findByAndCondition() {
        // 创建条件
        Criteria criteriaUserName = Criteria.where("userName").is("张三");
        Criteria criteriaPassWord = Criteria.where("passWord").is("123456");
        // 创建条件对象,将上面条件进行 AND 关联
        Criteria criteria = new Criteria().andOperator(criteriaUserName, criteriaPassWord);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据【OR】关联多个查询条件,查询集合中的文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据【OR】关联多个查询条件,查询集合中的文档数据
     */
    @Test
    public void findByOrCondition() {
        // 创建条件
        Criteria criteriaUserName = Criteria.where("userName").is("张三");
        Criteria criteriaPassWord = Criteria.where("passWord").is("123456");
        // 创建条件对象,将上面条件进行 OR 关联
        Criteria criteria = new Criteria().orOperator(criteriaUserName, criteriaPassWord);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据【IN】关联多个查询条件,查询集合中的文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据【IN】关联多个查询条件,查询集合中的文档数据
     */
    @Test
    public void findByInCondition() {
        // 设置查询条件参数
        List ids = Arrays.asList(1l, 10l, 11l);
        // 创建条件
        Criteria criteria = Criteria.where("id").in(ids);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据【逻辑运算符】查询集合中的文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据【逻辑运算符】查询集合中的文档数据
     */
    @Test
    public void findByOperator() {
        // 设置查询条件参数
        int min = 20;
        int max = 35;
        Criteria criteria = Criteria.where("age").gt(min).lte(max);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据【正则表达式】查询集合中的文档数据
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据【正则表达式】查询集合中的文档数据
     */
    @Test
    public void findByRegex() {
        // 设置查询条件参数
        String regex = "^张*";
        Criteria criteria = Criteria.where("userName").regex(regex);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据条件查询集合中符合条件的文档,获取其文档列表并排序
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据条件查询集合中符合条件的文档,获取其文档列表并排序
     */
    @Test
    public void findByConditionAndSort() {
        String userName = "张三";
        Query query = new Query(Criteria.where("userName").is(userName)).with(Sort.by("age"));
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 根据单个条件查询集合中的文档数据,并按指定字段进行排序与限制指定数目
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

   /**
     * 根据单个条件查询集合中的文档数据,并按指定字段进行排序与限制指定数目
     */
    @Test
    public void findByConditionAndSortLimit() {
        String userName = "张三";
        //从第一行开始,查询2条数据返回
        Query query = new Query(Criteria.where("userName").is(userName)).with(Sort.by("createTime")).limit(2).skip(1);
        List result = mongoTemplate.find(query, Person.class);
        System.out.println("查询结果:" + result.toString());
    }
}
  • 统计集合中符合【查询条件】的文档【数量】
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

   /**
     * 统计集合中符合【查询条件】的文档【数量】
     */
    @Test
    public void countNumber() {
        // 设置查询条件参数
        String regex = "^张*";
        Criteria criteria = Criteria.where("userName").regex(regex);
        // 创建查询对象,然后将条件对象添加到其中
        Query query = new Query(criteria);
        long count = mongoTemplate.count(query, Person.class);
        System.out.println("统计结果:" + count);
    }
}

索引在所有的数据库中,暂居的位置非常重要,例如当你检索一张上百万的数据表的时候,如果没走索引,查询效率会极其缓慢,对于 MongoDB 来说,同样如此。

示例如下:

  • 创建升序索引
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 创建升序索引
     */
    @Test
    public void createAscendingIndex() {
        // 设置字段名称
        String field = "userName";
        // 创建索引
        mongoTemplate.getCollection("persons").createIndex(Indexes.ascending(field));
    }
}
  • 移除索引
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

   /**
     * 根据索引名称移除索引
     */
    @Test
    public void removeIndex() {
        // 设置字段名称
        String field = "userName";
        // 删除索引
        mongoTemplate.getCollection("persons").dropIndex(field);
    }
}
  • 查询集合中所有的索引
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

   /**
     * 查询集合中所有的索引
     */
    @Test
    public void getIndexAll() {
        // 获取集合中所有列表
        ListIndexesIterable indexList = mongoTemplate.getCollection("persons").listIndexes();
        // 获取集合中全部索引信息
        for (Document document : indexList) {
            System.out.println("索引列表:" + document);
        }
    }
}
  • 我们还可以通过在实体类上加注解方式来创建索引
/**
 * 使用@Document注解指定集合名称
 */
@Document(collection="persons")
public class Person implements Serializable {
    private static final long serialVersionUID = -3258839839160856613L;

    /**
     * 使用@Id注解指定MongoDB中的 _id 主键
     */
    @Id
    private Long id;

    private String userName;

    private String passWord;

    private Integer age;

    /**
     * 创建一个5秒之后文档自动删除的索引
     */
    @Indexed(expireAfterSeconds=5)
    private Date createTime;

    //...get/set

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", age=" + age +
                ", createTime=" + createTime +
                '}';
    }
}

单节点 mongodb 不支持事务,需要搭建 MongoDB 复制集。

/**
 * 配置事务管理器
 *
 */
@Configuration
public class TransactionConfig {

    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

}

事务服务测试!

@Service
public class TransactionExample {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Transactional(rollbackFor = Exception.class)
    public Object transactionTest(){
        Person person =new Person();
        person.setId(1l);
        person.setUserName("张三");
        person.setPassWord("123456");
        person.setCreateTime(new Date());
        Person newPerson = mongoTemplate.insert(person);
        // 抛出异常,观察数据是否进行回滚
        if(1 == 1){
            throw new RuntimeException("异常");
        }
        return newPerson;
    }
}

本文主要围绕 MongoDB 的 Java 客户端使用进行基本的增删改查操作介绍,在实际的业务场景中,可能还需要用到聚合函数等高级查询,大家如果有这种需求,可以访问如下地址获取更加详细的 api 文档介绍:MongoDB 文档查询 api 介绍

Original: https://www.cnblogs.com/dxflqm/p/16643981.html
Author: 程序员志哥
Title: SpringBoot 整合 MongoDB 实战解说

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/612307/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • FlyAway–数据库版本管理工具

    相关博文: FlyAway官方文档: https://flywaydb.org/documentation/getstarted/how 关于SpringBoot整合并使用FlyA…

    数据库 2023年6月6日
    063
  • 分析了2020年3万多条的微博热搜,我看到了什么

    前言 2020年是艰难的一年,但即使再难,也都过去了。 分析一下2020年的新闻热搜,可以大致了解网民都在关注什么。 微博热搜以娱乐为主,头条的热搜更偏向民生与时事。今天,我们先分…

    数据库 2023年6月16日
    097
  • Java 多线程共享模型之管程(下)

    共享模型之管程 wait、notify wait、notify 原理 Owner 线程发现条件不满足,调用 wait 方法,即可进入 WaitSet 变为 WAITING 状态 B…

    数据库 2023年6月16日
    0113
  • javaScript知识大全(基础)!!

    定义方法:即把函数放在对象里面,对象只有属性和方法 var a={ name:"111", birth:2000, age:function (){ var n…

    数据库 2023年6月16日
    077
  • Logback实现按业务输出到对应日志文件

    一、方案 由于需要按业务生成不同的日志文件,看到按业务来区分,我的第一感觉就是业务其实是可以按包名来区分的。所以其实我们只要实现不同的包下面的日志输出到不同的文件,就能实现需求了。…

    数据库 2023年6月6日
    0276
  • Zookeeper应用场景汇总(超详细)

    Zookeeper典型应用场景汇总 数据发布与订阅(配置中心) 发布与订阅模型,即所谓的配置中心,顾名思义就是发布者将数据发布到ZK节点上,供订阅者动态获取数据,实现配置信息的集中…

    数据库 2023年6月11日
    085
  • vue2框架基础

    一、什么是vue? vue是一个优秀的前端框架,他与Angular.js、React.js成为前端三大主流框架。他是一套构建用户界面的框架,只关注视图层,可以完成大型项目的开发,框…

    数据库 2023年6月14日
    088
  • MySQL&MariaDB数据库备份脚本

    MySQL&MariaDB数据库备份脚本 MySQL&MariaDB数据库备份脚本 MySQL & MariaDB .bat backup.sh MySQL…

    数据库 2023年6月9日
    079
  • [javaweb]重点总结大全

    javaweb web:网页静态web:html,css 数据不会发生变化动态web:servlet/jsp,asp,php每个人在不同的时间看到数据都不同 web应用程序编写完毕…

    数据库 2023年6月16日
    079
  • springboot~ApplicationContextAware与@Autowired注解

    ApplicationContextAware是一个接口,它提供一个方法setApplicationContext,当spring注册完成之后,会把ApplicationConte…

    数据库 2023年6月6日
    082
  • MySQL特性:MRR,Multi-Range Read,多范围读

    孔个个MRR在5.6版本开始支持,相关文章不少。但是读起来层次感差了一些,在这里我用自己的理解重新整理了一版。这里参考了很多在网络上能找到的资料,才使我更全面的理解MRR,但时间有…

    数据库 2023年6月16日
    056
  • JWT+SpringSecurity登录和权限管理

    一、什么是JWT 说起JWT,我们应该来谈一谈基于token的认证和传统的session认证的区别。说起JWT,我们应该来谈一谈基于token的认证和传统的session认证的区别…

    数据库 2023年6月6日
    095
  • 链表问题一些常用的套路与方法

    概述 链表问题应该是数据结构中比较基础的一类问题,但同时也是在面试中常考的一类问题。但是围绕链表问题的一些基本方法或者处理思想,也无外乎那几类,因此本文尝试对链表常用的一些方法或者…

    数据库 2023年6月11日
    079
  • 教师节我用Python做了个学生点名系统送给老师当礼物,这回毕业稳了

    今年教师节前夕,我特意用Python做了个学生点名系统,非常好用,送给各科老师、辅导员当节日礼物,老师们都喜滋滋,说平常逃课就原谅我了,我心想,这次毕业应该不是问题了~ 本文背景 …

    数据库 2023年6月14日
    084
  • jenkins-配置python

    1. 进入”Dashboard”界面,点击左侧”构建执行状态” 2. 点击列表设置图标 3. 勾选”Environmen…

    数据库 2023年6月14日
    067
  • kettle插入更新

    kettle实现若主键存在则更新,若主键不存在则插入 Original: https://www.cnblogs.com/cheng9999/p/14085922.htmlAuth…

    数据库 2023年6月16日
    070
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球