首先mongodb有自定义的主键,_id 类型为ObjectId、建议使用mongodb自定义的主键来做主键
原因 1、信息更丰富 2、据说效率会更高
/**
* _id 出现
* _id 5b6aa72712df523ceca5f469
* 16进制转10进制
* 前8位 5b6aa727 1533716263 → 2018/8/8 16:17:43
* 9-14位 12df52 1236818 机器唯一标示
* 15-18位 3cec 15596 mongodb进程号
* 19-24 a5f469 10876009 自增计数器
*/
cloud为mongodb的操作封装了两个主要的工具类MongoTemplate和GridFsTemplate,但是连接池以及相关时间类参数无法设置。
程序猿DD大神简单封装了一下MongoClientOptions,咱们就可以对这些参数进行操作了,当然也可以自己实现,引用pom
操作spring.data.mongodb.option.*
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.nykj.spring4all</groupId>
<artifactId>mongodb-plus-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
mongodb的简单实体,用到了spring mongodb相关的@Document @Indexd 等注解,需要注意的是@DBRef,具体需要了解可百度
参考链接:https://www.cnblogs.com/liuchuanfeng/p/7772421.html
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document
public class ApplyInfo {
@Indexed(unique = true)
private String key;
@Indexed
private String applyNo;
private String name;
@Indexed
private String idNumber;
private String bankCard;
private String tel;
@Indexed
private String type;
private long createTime = System.currentTimeMillis();
private String content;
}
简单的使用MongoTemplate进行操作,使用JsonObject和ApplyInfo区别在于用JsonObject可以查出_id的相关信息,供参考。
public void save(ApplyInfo applyInfo) {
mongoTemplate.insert(applyInfo);
}
public List<JSONObject> getApplyInfo(String applyNo) {
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("applyNo").is(applyNo));
Query query = new Query(criteria);
List<JSONObject> ApplyInfoList = mongoTemplate.find(query, JSONObject.class, "applyInfo");
return ApplyInfoList;
}
public List<ApplyInfo> getApplyInfo2(String applyNo) {
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("applyNo").is(applyNo));
Query query = new Query(criteria);
List<ApplyInfo> ApplyInfoList = mongoTemplate.find(query, ApplyInfo.class);
return ApplyInfoList;
}
使用实体类会在mongo的数据中生成一个_class字段,包含了package路径,可以去除_class
闲麻烦没去除,有优雅的方式可告知。
有些尚未验证,欢迎来踩:
注意点①insert方法、唯一键/主键 重复报错、save方法覆盖更新(批量插入遍历,性能较慢)
②分片键的选择要切合业务做设计同时分片键约散分片会越好一点,只能用唯一索引做分片键,不然会报错。
组合索引唯一、分片也必须用它。
@CompoundIndexes(@CompoundIndex(name = "apply_key_index", def = "{'applyNo': 1, 'type': 1}", unique = true))
db.runCommand( { enablesharding :"testdb"});、
db.runCommand( { shardcollection : "test.applyInfo",key : {type:1,_id:1} } )
③ An upsert on a sharded collection must contain the shard key and have the simple collation
upsert时候必须包含分片键,不然会失败,upsert不方便用来更新记录,除非update设置每个field
// Query query = Query.query(Criteria.where("key").is(applyInfo.getKey()));
// Update update = Update.update("applyContent",applyInfo.getApplyContent());
// mongoTemplate.upsert(query, update, applyInfo.getClass());
④save的底层是upsert,id和_id不一样,分片键不要用id,不存在的field可以作为分片键。。。
db.runCommand( { shardcollection : "test.applyInfo",key : {id:1} } )