1.拉取镜像
docker pull mongo:latest
docker run -d --restart=always -p 27017:27017 --name mymongo -v /docker/mongodb/data:/data/db -d mongo
3.查看容器是否运行成功
docker ps -a
SQL术语/概念 | MongoDB术语/概念 | 解释说明 |
---|---|---|
database | database | database |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
table | joins | 表连接,MongoDB不支持 |
7.注意事项
1、文档中的键/值对是有序的。
2、文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
3、MongoDB区分类型和大小写。
4、MongoDB的文档不能有重复的键。
5、文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。
6、键不能含有\0 (空字符)。这个字符用来表示键的结尾。
7、.和$有特别的意义,只有在特定环境下才能使用。
8、以下划线"_"开头的键是保留的(不是严格要求的)。
1.Help查看命令提示
db.help();
2.切换数据库
use test
当数据库不存在时会创建
3.删除数据库
db.dropDatabase();
4.获取数据库名称
db.getName();
5.查看版本
db.version();
6.创建集合
db.createCollection("student")
7.新增字段
db.student.save({name:'李华',age:0,sex:'男'})
1.当第二次使用命令时新增字段时,之前新增的数据会为空
db.student.save({name:'李华',age:0,sex:'男',birthyday:'2022-02-23'})
8.查询
# select * from student where name = 'zhangsan'
db.student.find({name:"zhangsan"})
# select name, age from student where age = 21
db.student.find({age:21}, {'name':1, 'age':1})
9.排序
# select * from student order by age
db.student.find().sort({age:1})
在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
10.截取
# select * from student skip 2 limit 3
db.student.find().skip(0).limit(3)
在 MongoDB 中使用 limit()方法来读取指定数量的数据,skip()方法来跳过指定数量的数据
11.in
# select * from student where age in (21, 26, 32)
db.student.find({age:{$in:[21,26,32]}})
12.count
# select count(*) from student where age >20
db.student.find({age:{$gt:20}}).count()
13.or
# select * from student where age = 21 or age = 28
db.student.find({$or:[{age:21}, {age:28}]})
14.update
# update student set age = 100, sex = 0 where name = 'zhangsan'
db.student.update({name:"zhangsan"}, {$set:{age:100, sex:0}})
Update()有几个参数需要注意。
db.collection.update(criteria, objNew, upsert, mult)
criteria:需要更新的条件表达式
objNew:更新表达式
upsert:如FI标记录不存在,是否插入新文档。
multi:是否更新多个文档。
15.删除
db.student.remove(id)
//移除对应id的行
db.student.remove({})
//移除所有
1.引入相关依赖pom文件
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-mongodb-start</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependency>
</dependencies>
</project>
2.添加配置文件
spring:
data:
mongodb:
uri: mongodb://192.168.32.250:27017/studyinfo
server:
port: 8009
3.新建实体类
package com.example.domain;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(collection = "student")
public class Student {
@Id
private String id;
private String name;
private int age;
private String sex;
}
4.新建test类
import com.example.MyMongodbApplication;
import com.example.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest(classes = MyMongodbApplication.class)
@RunWith(SpringRunner.class)
public class MongodbTest {
//引入依赖
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void query(){
List<Student> student = mongoTemplate.findAll(Student.class, "student");
student.forEach(System.out::println);
}
@Test
public void insert(){
mongoTemplate.insert(new Student("韩信",21,"男"));
}
@Test
public void update(){
Query query=new Query();
Criteria criteria = Criteria.where("name").is("李白");
query.addCriteria(criteria);
List<Student> students = mongoTemplate.find(query, Student.class);
students.forEach(System.out::println);
Update update=new Update();
update.set("name","李白2");
mongoTemplate.upsert(query,update,Student.class);
}
@Test
public void del(){
Student byId = mongoTemplate.findById("63f9e6297a836d86c4cd5bc2", Student.class);
mongoTemplate.remove(byId);
}
}
5.以上就是使用MongoTemplate 完成CURD,特别注意的是,查询修改删除中都要使用的query对象,以及条件构造器criteria对象进行操作。
也可以使用MongoRepository,使用方法与MybatisPlus用法很像,有兴趣的小伙伴可以自行研究