mongo 分布式nosql 数据库
特点:基于硬盘创建的虚拟内存,有点不用占用太多内存,缺点速度不如redis快
每次增删改查需要初始化后操作
内存存储的为gson数据
mongo shell :支持js语法
mongo 初始化shell : rs.initiate()
docker-compose 启动
mongodb:
container_name: sc-mongo
image: mongo
ports:
- 27017:27017
networks:
- sc-net
volumes:
- local_mongodb_data_v1:/data/db
networks:
sc-net:
external: false
volumes:
local_mongodb_data_v1:
springboot2.x 操作mongo
构建一个稍复杂的类goods
@Document("goods")
public class Goods {
@Id
private String id;
private String name;
private Category category;
private List<Sku> skuList;
public static class Category {
private String id;
private String name;
public Category(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Sku {
private String id;
private String name;
private Double price;
public Sku(String id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public List<Sku> getSkuList() {
return skuList;
}
public void setSkuList(List<Sku> skuList) {
this.skuList = skuList;
}
}
springboot 使用mongo
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
spring:
application:
name: spirng-boot-mongodb
data:
mongodb:
host: localhost #同127.0.0.1
port: 27017
database: test #指定操作的数据库
使用
@Autowired
MongoTemplate mongoTemplate
一些案列:
1.插入数据
Goods goods = new Goods();
goods.setName("教科书");
goods.setCategory(new Goods.Category("1","书籍"));
goods.setSkuList(new ArrayList<Goods.Sku>(){{
add(new Goods.Sku("1","语文",19.8));
add(new Goods.Sku("2","数学",11.1));
add(new Goods.Sku("3","英语",9.6));
}});
mongoTemplate.insert(goods);
2.查寻分类id为1的goods
List<Goods> goodsList = mongoTemplate
.find(Query.query(Criteria.where("category._id").is("1"))
, Goods.class);
3.查询skuid为1的数据
MatchOperation match = Aggregation.match(Criteria.where("skuList._id").in("1"));
UnwindOperation skuList = Aggregation.unwind("skuList");
ArrayList<AggregationOperation> aggregationOperations = new ArrayList<>();
aggregationOperations.add(skuList);
aggregationOperations.add(match);
GroupOperation groupOperation = Aggregation.group("_id").first("name")
.as("name").first("category").as("category")
.push("skuList").as("skuList");
aggregationOperations.add(groupOperation);
Aggregation aggregation = Aggregation.newAggregation(aggregationOperations);
AggregationResults<Goods> goods = mongoTemplate.aggregate(aggregation, "goods", Goods.class);
List<Goods> results = goods.getMappedResults();
网友评论