资料地址:
链接:https://pan.baidu.com/s/1DxgX3RCvWfaKzziHG8IHZQ
提取码:3452
gitee地址
https://gitee.com/liushanshan126/mongodb-test



进入bin文件夹中,cmd控制台,输入命令:
mongod–dbpath=…\data\db

注意:创建数据库,但是这个数据库没有集合(表)的时候,只会存在于内存中,只有当当前的数据库中有集合的时候才会实例化到磁盘中
进入需要删除的数据库,然后执行
db.dropDatabase()
db.comment.insert({“articleid”:“100000”,“content”:“今天天气真好,阳光明媚”,“userid”:“1001”,“nickname”:“Rose”,“createdatetime”:newDate(),“likenum”:NumberInt(10),“state”:null})
例如: db.article.findOne({articleid:‘12222’}, {userid:1, nickname:1}) 查询字段articleid为12222的数据,且只拿出userid,nickname, _id(默认)字段
db.article.update({_id:“2”},{$set:{likenum:NumberInt(889)}})
db.article.update({_id:“3”},{$inc:{likenum:NumberInt(1)}})

sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
db.COLLECTION_NAME.find().sort({KEY:1})
跟mysql一样,先执行sort,在执行skip,最后执行limit


in:db.comment.find({userid:{KaTeX parse error: Expected 'EOF', got '}' at position 19: …["1003","1004"]}̲}) nin:db.comme…nin:[“1003”,“1004”]}})

选择切换数据库:usearticledb
插入数据:db.comment.insert({bson数据})
查询所有数据:db.comment.find();
条件查询数据:db.comment.find({条件})
查询符合条件的第一条记录:db.comment.findOne({条件})
查询符合条件的前几条记录:db.comment.find({条件}).limit(条数)
查询符合条件的跳过的记录:db.comment.find({条件}).skip(条数)
修改数据:db.comment.update({条件},{修改后的数据})或db.comment.update({条件},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …t.update({条件},{inc:{自增的字段:步进值}})
删除数据:db.comment.remove({条件})
统计查询:db.comment.count({条件})
模糊查询:db.comment.find({字段名:/正则表达式/})
条件比较运算:db.comment.find({字段名:{$gt:值}})包含查询:db.comment.find({字段名:{KaTeX parse error: Expected 'EOF', got '}' at position 11: in:[值1,值2]}̲})或db.comment.f…nin:[值1,值2]}})
条件连接查询:db.comment.find({KaTeX parse error: Expected 'EOF', got '}' at position 18: …d:[{条件1},{条件2}]}̲)或db.comment.fi…or:[{条件1},{条件2}]})
单字段索引、复合索引、其他索引
查看:db.collection.getIndexes()
创建:
单字段索引:db.comment.createIndex({userid:1})
复合索引:db.comment.createIndex({userid:1,nickname:-1})
删除:
单个删除:db.comment.dropIndex({userid:1})
删除所有索引:db.collection.dropIndexes()

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
<!--lookupparentfromrepository-->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--连接mongodb的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>


package com.bear.service;
import com.bear.dao.CommentRepository;
import com.bear.pojo.Comment;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Optional;
/**
* <简述>
* <详细描述>
*
* @author LiuShanshan
* @version $Id$
*/
@Service
public class CommentService {
@Resource
private CommentRepository commentRepository;
/**
*<简述> 增加
*<详细描述>
* @author Liushanshan
* @param comment
* @return void
*/
public void saveComment(Comment comment){
Comment save = commentRepository.save(comment);
System.out.println(save);
}
// 删
public void delete(String id){
commentRepository.deleteById(id);
}
// 改
public void update (Comment comment){
commentRepository.save(comment);
}
// 查
public Comment select (String id){
Optional<Comment> byId = commentRepository.findById(id);
Comment comment = byId.get();
return comment;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes=MongodbApplication.class)
public class Test {
@Autowired
private CommentService commentService;
// 保存
@org.junit.Test
public void saveTest(){
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);
}
// 查询
@org.junit.Test
public void select(){
Comment select = commentService.select("62bd78cc4e03471050ba849e");
System.out.println(select);
}
// 更新
@org.junit.Test
public void update(){
Comment select = commentService.select("62bd78cc4e03471050ba849e");
select.setUserid("234234234");
commentService.update(select);
}
// 删除
}


