MongoDB-基础使用(二)

前置文章:
MongoDB-基础使用(一),该文主要介绍MongoDB概念、安装、库/集合(表)/文档(行)操作。

零、本文纲要

一、索引

  1. 索引类型
  2. 索引管理操作
  3. 索引使用

二、API操作-快速入门

  1. pom.xml
  2. application.yml
  3. 启动类
  4. 测试
  5. 创建测试集合实体类
  6. 持久层接口&业务层编写
  7. 测试
  8. 根据上级ID查询分页列表
  9. MongoTemplate

一、索引

MongoDB索引使用B树数据结构(确切的说是B-Tree,MySQL是B+Tree)

注意:该部分的内容与Redis、JS类似,整体特性又与MySQL相近。可以简单了解以下。

1. 索引类型

a、单字段索引
b、复合索引
c、地理空间索引
d、文本索引
e、哈希索引

2. 索引管理操作

① 索引查看

# 索引查看
db.collection.getIndexes()
#如:
db.collection_test.getIndexes()
> db.collection_test.getIndexes()
[
    {
        "v" : 2,                            #索引的版本号
        "key" : {                           #索引的字段
            "_id" : 1                       #1代表升序
        },
        "name" : "_id_",                    #索引的名称,默认为 "字段名_" + "升降序规则数字",如:"userid_1_nickname_-1"
        "ns" : "db_test.collection_test"    #命名空间,数据库.集合
    }
]

② 索引创建

# 创建单字段索引
db.collection.createIndex(keys, options)
#如:
db.collection_test.createIndex({userid: 1}) #此处1代表升序,-1则为降序,单字段索引升降序不影响整体查询效率

# 创建复合索引
#如:
db.collection_test.createIndex({userid: 1, nickname: -1})

以下了解即可:

参数 类型 描述
background Boolean 建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索引,即增加 "background" 可选参数。 "background" 默认值为false
unique Boolean 建立的索引是否唯一。指定为true创建唯一索引。默认值为false.
name string 索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。
dropDups Boolean 3.0+版本已废弃。在建立唯一索引时是否删除重复记录,指定 true 创建唯一索引。默认值为 false.
sparse Boolean 对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.
expireAfterSeconds integer 指定一个以秒为单位的数值,完成 TTL设定,设定集合的生存时间。
v index version 索引的版本号。默认的索引版本取决于mongod创建索引时运行的版本。
weights document 索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。
default_language string 对于文本索引,该参数决定了停用词及词干和词器的规则的列表。 默认为英语
language_override string 对于文本索引,该参数指定了包含在文档中的字段名,语言覆盖默认的language,默认值为 language.

③ 索引移除

# 索引移除
db.collection.dropIndex(index)
#如:
db.collection_test.dropIndex("userid_1")
db.collection_test.dropIndex({userid: 1})

# 移除所有索引(默认"_id_"索引不会被移除)
db.collection_test.dropIndexes()

3. 索引使用

① 执行计划

# 执行计划
db.collection.find(query,options).explain(options)
#如:
db.collection_test.find({userid: "1003"}).explain()

"stage" : "COLLSCAN", 表示全集合扫描
"stage" : "IXSCAN" ,基于索引的扫描

② 涵盖查询(索引覆盖)

db.collection_test.find(
    {userid: "1003"},
    {userid:1,_id:-1}
).explain()

二、API操作-快速入门

注意:该部分内容与JPA、MyBatis类似,简单了解即可。使用的时候能查询资料,能看懂就可以。

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.stone</groupId>
    <artifactId>mongodb-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <lombok.version>1.18.22</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>

</project>

2. application.yml

spring:
#数据源配置
  data:
    mongodb:
      # 主机地址
      host: 192.168.253.128
      # 数据库
      database: test_db
      # 默认端口是27017
      port: 27017
      #也可以使用uri连接
      #uri: mongodb://192.168.253.128:27017/test_db

3. 启动类

@SpringBootApplication
public class MongodbApplication {
    public static void main(String[] args) {
        SpringApplication.run(MongodbApplication.class, args);
    }
}

4. 测试

此时,只要能正常连接,不报错即可。

5. 创建测试集合实体类

① @Document注解

类名小写与集合名一致则可以省略属性配置,如果省略,则默认使用类名小写映射集合;
假设集合名为"comment",则直接使用@Document即可。

② @CompoundIndex注解

@CompoundIndex(def = "{'userid': 1, 'nickname': -1}"),用于指定复合索引;
还是推荐在client通过指令提前生成索引,不推荐使用注解生成索引。

③ @Id注解

主键标识,该属性的值会自动对应mongodb的主键字段"_id";
如果该属性名就叫“id”,则该注解可以省略,否则必须写。

④ @Field注解

该属性对应mongodb的字段的名字,如果一致,则无需该注解。

⑤ @Indexed注解

添加了一个单字段的索引;
还是推荐在client通过指令提前生成索引,不推荐使用注解生成索引。

/**
 * 文章评论实体类
 */
//把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
//@Document(collection="mongodb 对应 collection 名")
// 若未加 @Document ,该 bean save 到 mongo 的 comment collection
// 若添加 @Document ,则 save 到 comment collection
@Document(collection="collection_test")//类名小写与集合名一致则可以省略,如果省略,则默认使用类名小写映射集合
//复合索引
// @CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
@Data //lombok的注解
public class Comment implements Serializable {
    //主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写
    @Id //此处其实可以省略
    private String id;//主键
    //该属性对应mongodb的字段的名字,如果一致,则无需该注解
    @Field("content") //此处该注解其实可以省略
    private String content;//吐槽内容
    private Date publishtime;//发布日期
    //添加了一个单字段的索引
    @Indexed
    private String userid;//发布人ID
    private String nickname;//昵称
    private LocalDateTime createdatetime;//评论的日期时间
    private Integer likenum;//点赞数
    private Integer replynum;//回复数
    private String state;//状态
    private String parentid;//上级ID
    private String articleid;
}

6. 持久层接口&业务层编写

① 持久层接口

该接口需要继承MongoRepository接口,并指定实体类、主键类型,如下:

public interface CommentRepository extends MongoRepository<Comment, String> {
}

MongoRepository接口,如下:

@NoRepositoryBean
public interface MongoRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

    @Override
    <S extends T> List<S> saveAll(Iterable<S> entities);

    @Override
    List<T> findAll();

    @Override
    List<T> findAll(Sort sort);

    <S extends T> S insert(S entity);

    <S extends T> List<S> insert(Iterable<S> entities);

    @Override
    <S extends T> List<S> findAll(Example<S> example);

    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);

可以看到基础的插入、查询操作都有,运行时则会生成其代理对象执行具体方法。

② 业务层

public interface CommentService {
    public void saveComment(Comment comment);
    public void updateComment(Comment comment);
    public void deleteCommentById(String id);
    public List<Comment> findCommentList();
    public Comment findCommentById(String id);
}

/**
 * 评论的业务层
 */
@Service
public class CommentServiceImpl implements CommentService {

    //注入dao
    @Autowired
    private CommentRepository commentRepository;

    /**
     * 保存一个评论
     * @param comment
     */
    public void saveComment(Comment comment){
        //如果需要自定义主键,可以在这里指定主键;如果不指定主键,MongoDB会自动生成主键
        //设置一些默认初始值。。。
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 更新评论
     * @param comment
     */
    public void updateComment(Comment comment){
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 根据id删除评论
     * @param id
     */
    public void deleteCommentById(String id){
        //调用dao
        commentRepository.deleteById(id);
    }

    /**
     * 查询所有评论
     * @return
     */
    public List<Comment> findCommentList(){
        //调用dao
        return commentRepository.findAll();
    }

    /**
     * 根据id查询评论
     * @param id
     * @return
     */
    public Comment findCommentById(String id){
        //调用dao
        return commentRepository.findById(id).get();
    }
}

7. 测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongodbApplication.class)
public class CommentServiceImplTest {
    //注入Service
    @Autowired
    private CommentService commentService;
    /**
     * 保存一个评论
     */
    @Test
    public void testSaveComment(){
        Comment comment=new Comment();
        comment.setArticleid("100000");
        comment.setContent("测试添加的数据");
        comment.setCreatedatetime(LocalDateTime.now());
        comment.setUserid("1003");
        comment.setNickname("凯撒大帝");
        comment.setState("1");
        comment.setLikenum(0);
        comment.setReplynum(0);
        commentService.saveComment(comment);
    }
    /**
     * 查询所有数据
     */
    @Test
    public void testFindAll(){
        List<Comment> list = commentService.findCommentList();
        System.out.println(list);
    }
    /**
     * 测试根据id查询
     */
    @Test
    public void testFindCommentById(){
        Comment comment = commentService.findCommentById("62976eb31629ea868c6b5511");
        System.out.println(comment);
    }
}

8. 根据上级ID查询分页列表

这个地方非常特殊,必须要在实体类内设置parentid属性,然后跟据该属性进行分页。

① CommentRepository新增方法定义

//根据父id,查询子评论的分页列表
Page<Comment> findByParentid(String parentid, Pageable pageable);

② CommentService新增方法

public Page<Comment> findCommentListPageByParentid(String parentid, int page, int size);
/**
* 根据父id查询分页列表
* @param parentid
* @param page
* @param size
* @return
*/
public Page<Comment> findCommentListPageByParentid(String parentid, int page, int size){
    return commentRepository.findByParentid(parentid, PageRequest.of(page - 1, size));
}

③ 测试

注意:一定要先插入带有parentid属性的数据再测试

/**
 * 测试根据父id查询子评论的分页列表
 */
@Test
public void testFindCommentListPageByParentid(){
    Page<Comment> pageResponse = commentService.findCommentListPageByParentid("3", 1, 2);
    System.out.println("----总记录数:"+pageResponse.getTotalElements());
    System.out.println("----当前页数据:"+pageResponse.getContent());
}

9. MongoTemplate

继承MongoRepository接口默认只能实现基础的查询、插入、保存操作,而且较为复杂的操作需要先查询、再设置新数据、最后保存,效率较低。

/**
* 点赞-效率低
* @param id
*/
public void updateCommentThumbupToIncrementingOld(String id){
    Comment comment = CommentRepository.findById(id).get();
    comment.setLikenum(comment.getLikenum() + 1);
    CommentRepository.save(comment);
}

使用MongoTemplate类可以提升此效率,用法如下:

public void updateCommentLikenum(String id);


//注入MongoTemplate
@Autowired
private MongoTemplate mongoTemplate;

/**
 * 点赞数+1
 *
 * @param id
 */
@Override
public void updateCommentLikenum(String id) {
    //查询对象
    Query query = Query.query(Criteria.where("_id").is(id));
    //更新对象
    Update update = new Update();
    //局部更新,相当于$set
    // update.set(key,value)
    //递增$inc
    // update.inc("likenum",1);
    update.inc("likenum");
    //参数1:查询对象
    //参数2:更新对象
    //参数3:集合的名字或实体类的类型Comment.class
    mongoTemplate.updateFirst(query, update, "collection_test");
}

测试:

/**
* 点赞数+1
*/
@Test
public void testUpdateCommentLikenum(){
    //对3号文档的点赞数+1
    commentService.updateCommentLikenum("62976eb31629ea868c6b5511");
}

三、结尾

以上即为MongoDB-基础使用(二)的全部内容,感谢阅读。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容