索引
//查看索引
db.collection.getIndexes()
結果中顯示的是默認 _id 索引。
db.comment.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "articledb.comment"
}
]
//創建索引,1 表示升序
db.comment.createIndex({userid:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
//復合索引
db.comment.createIndex({userid:1,nickname:-1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
//刪除 comment 集合中 userid 字段上的升序索引
db.comment.dropIndex({userid:1})
//刪除所有索引
db.comment.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
spring boot 使用 mongodb 實現文章評論功能
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
spring:
#數據源配置
data:
mongodb:
# 主機地址
host: 127.0.0.1
# 數據庫
database: articledb
# 默認端口是27017
port: 27017
#也可以使用uri連接
#uri: mongodb://127.0.0.1:27017/articledb
@Document(collection="comment")//可以省略,如果省略,則默認使用類名小寫映射集合
//復合索引
// @CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
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;
- 數據訪問接口,extends MongoRepository<Comment(實體類名),String(主鍵類型)>
//評論的持久層接口
public interface CommentRepository extends MongoRepository<Comment,String> {
}
//評論的業務層
@Service
public class 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();
}
}
//CommentRepository新增方法定義,findByParentid 方法名有要求
//根據父id,查詢子評論的分頁列表
Page<Comment> findByParentid(String parentid, Pageable pageable);
//CommentService新增方法
/**
* 根據父id查詢分頁列表
*/
public Page<Comment> findCommentListPageByParentid(String parentid,int page ,int size){
return commentRepository.findByParentid(parentid, PageRequest.of(page-1,size));
}
/**
* 測試根據父id查詢子評論的分頁列表
*/
@Test
public void testFindCommentListPageByParentid(){
Page<Comment> pageResponse = commentService.findCommentListPageByParentid("3", 1, 2);
System.out.println("----總記錄數:"+pageResponse.getTotalElements());
System.out.println("----當前頁數據:"+pageResponse.getContent());
}
點贊
//修改CommentService
//注入MongoTemplate
@Autowired
private MongoTemplate mongoTemplate;
/**
* 點贊數+1
* @param id
*/
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,"comment");
}