当前位置:网站首页>mongodb(快速上手)(一)
mongodb(快速上手)(一)
2022-07-05 16:57:00 【进击的北极熊】
资料地址:
链接:https://pan.baidu.com/s/1DxgX3RCvWfaKzziHG8IHZQ
提取码:3452
gitee地址
https://gitee.com/liushanshan126/mongodb-test
一、mongdb相关概念
1、mysql和mongdb对比


2、bson(mongdb存储结构,与json类似)
- Bson中,除了基本的JSON类型:string,integer,boolean,double,null,array和object
- mongo还使用了特殊的数据类型。这些类型包括date,object id,binary data,regular expression
和code。每一个驱动都以特定语言的方式实现了这些类型,查看你的驱动的文档来获取详细信息。
二、单机部署
1、安装压缩文件,并且创建data/db文件
2、单机启动命令
进入bin文件夹中,cmd控制台,输入命令:
mongod–dbpath=…\data\db
三、基本常用命令
1、创建、显示数据库

注意:创建数据库,但是这个数据库没有集合(表)的时候,只会存在于内存中,只有当当前的数据库中有集合的时候才会实例化到磁盘中
2、删除
进入需要删除的数据库,然后执行
db.dropDatabase()
3 、插入数据
db.comment.insert({“articleid”:“100000”,“content”:“今天天气真好,阳光明媚”,“userid”:“1001”,“nickname”:“Rose”,“createdatetime”:newDate(),“likenum”:NumberInt(10),“state”:null})
3、查询db.document.find({},{}):document为表;第一个大括号:查询条件,相当于where后面的条件;第二个大括号:显示哪些字段,相当于select后面的查询字段
例如: db.article.findOne({articleid:‘12222’}, {userid:1, nickname:1}) 查询字段articleid为12222的数据,且只拿出userid,nickname, _id(默认)字段
4、更新
4.1 、 普通更新(局部更新$set)
db.article.update({_id:“2”},{$set:{likenum:NumberInt(889)}})
4.2 列值增加 (数字 + n) $inc
db.article.update({_id:“3”},{$inc:{likenum:NumberInt(1)}})
5、删除文档

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

8、比较查询(大于,小于,大于等于,小于等于,不等于)

9、包含(in),不包含(nin)
in:db.comment.find({userid:{ KaTeX parse error: Expected 'EOF', got '}' at position 19: …["1003","1004"]}̲}) nin:db.comme…nin:[“1003”,“1004”]}})
10、条件连接。or和and的使用

总结
选择切换数据库: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}]})
四、索引
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()
3、使用索引作为条件,查询字段也为索引,则不查集合,直接查询索引

五、java实际操作mongodb
1、引入依赖
<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>
2、dao

3、实体类

4、service
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;
}
}
5、测试类
@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);
}
// 删除
}
6、分页条件查询

6.1 分页条件查询2.0

6.2 MongoTemplate的使用(***)

边栏推荐
- Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
- NPM installation
- 这个17岁的黑客天才,破解了第一代iPhone!
- [binary tree] insufficient nodes on the root to leaf path
- The first lesson of EasyX learning
- Embedded-c Language-3
- Complete solution instance of Oracle shrink table space
- [Jianzhi offer] 66 Build product array
- 【性能测试】全链路压测
- WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
猜你喜欢

Embedded-c Language-1

VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在

MySQL之知识点(七)

Three traversal methods of binary tree
Example tutorial of SQL deduplication

dried food! Semi supervised pre training dialogue model space

thinkphp3.2.3

Rider 设置选中单词侧边高亮,去除警告建议高亮

winedt常用快捷键 修改快捷键latex编译按钮

Error in composer installation: no composer lock file present.
随机推荐
ClickHouse(03)ClickHouse怎么安装和部署
Machine learning 01: Introduction
Function sub file writing
張平安:加快雲上數字創新,共建產業智慧生態
Cmake tutorial step6 (add custom commands and generate files)
EasyX second lesson
Tips for extracting JSON fields from MySQL
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
Use byte stream to read Chinese from file to console display
Machine learning 02: model evaluation
Embedded-c Language-3
Embedded UC (UNIX System Advanced Programming) -1
漫画:寻找无序数组的第k大元素(修订版)
【性能测试】jmeter+Grafana+influxdb部署实战
漫画:如何实现大整数相乘?(下)
华为云云原生容器综合竞争力,中国第一!
漫画:寻找股票买入卖出的最佳时机
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
机器学习01:绪论
Embedded-c language-6