一、基础类配置
- @NoRepositoryBean
- public interface IMongoRepository
extends MongoRepository { -
- String getTableName();
-
- void deleteAll(Collection
ids) ; -
- MongoOperations getMongoOperations();
-
- MongoEntityInformation
getEntityInformation(); -
- Class
getEntityClass(); -
- Class
getIdClass(); - }
- public class AbstractMongoRepository
extends SimpleMongoRepository implements IMongoRepository { -
- private final MongoOperations mongoOperations;
- private final MongoEntityInformation
entityInformation; -
- public AbstractMongoRepository(MongoEntityInformation
metadata, MongoOperations mongoOperations) { - super(metadata, mongoOperations);
- this.mongoOperations = mongoOperations;
- this.entityInformation = metadata;
- }
-
- @Override
- public String getTableName(){
- return entityInformation.getCollectionName();
- }
-
-
- @Override
- public void deleteAll(Collection
ids) { - ids.forEach(this::deleteById);
- }
-
-
- @Override
- public MongoOperations getMongoOperations() {
- return mongoOperations;
- }
-
- @Override
- public MongoEntityInformation
getEntityInformation() { - return entityInformation;
- }
-
- @Override
- public Class
getEntityClass() { - return entityInformation.getJavaType();
- }
-
- @Override
- public Class
getIdClass() { - return entityInformation.getIdType();
- }
- }
- @SpringBootApplication
- @EnableMongoRepositories(repositoryBaseClass = AbstractMongoRepository.class)
- public class MongoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(MongoApplication.class, args);
- }
- }
而、查询
(1)普通查询
- public interface UserRepository extends IMongoRepository
{ - //jpa根据名字自动适配查询
- List
findByName(String name); -
- List
findByNameAndAge(String name, int age); -
-
- //手动指定查询
- default List
listByNameAndAge(String name, int age){ -
- Query q = new Query(Criteria.where(User.Fields.name).is(name)
- .and(User.Fields.age).is(age));
-
- return this.getMongoOperations().find(q, this.getEntityClass());
- }
-
-
- default List
find(ObjectId id){ -
- Query q = new Query(Criteria.where("_id").is(id));
-
- return this.getMongoOperations().find(q, this.getEntityClass());
- }
-
- default Optional
find2(ObjectId id){ -
-
- return this.findById(id);
- }
-
- default List
listByAge(int ageMin,int ageMax) { - Query q = new Query(Criteria.where(User.Fields.name).exists(true)
- .andOperator(Criteria.where(User.Fields.age).gte(ageMin),
- Criteria.where(User.Fields.age).lt(ageMax)));
-
- return this.getMongoOperations().find(q, User.class, this.getTableName());
- }
-
- }
(2)排序
- public interface UserRepository extends IMongoRepository
{ -
-
- default List
listSort(int age){ -
- Query q = new Query(Criteria.where(User.Fields.age).is(age));
- q.with(Sort.by(Sort.Order.desc(User.Fields.name), Sort.Order.desc(User.Fields.age)));
-
- return this.getMongoOperations().find(q, this.getEntityClass());
- }
-
- }
(3)分页
- public interface UserRepository extends IMongoRepository
{ -
-
- default List
listLimit(int age){ -
- Query q = new Query(Criteria.where(User.Fields.age).is(age));
- q.skip(50).limit(10);
- return this.getMongoOperations().find(q, this.getEntityClass());
- }
-
- }
二、更新
- public interface UserRepository extends IMongoRepository
{ -
-
- default void update(User user){
-
- this.save(user);
- }
-
-
- default long updateAge(ObjectId id, int age){
-
- Query q = new Query(Criteria.where("_id").is(id));
- Update update = new Update();
- update.set(User.Fields.age, age);
- return this.getMongoOperations().updateFirst(q, update, getTableName()).getModifiedCount();
-
- }
-
- default long updateAge(String name, int age){
-
- Query q = new Query(Criteria.where(User.Fields.name).is(name));
- Update update = new Update();
- update.set(User.Fields.age, age);
- return this.getMongoOperations().updateMulti(q, update, getTableName()).getModifiedCount();
-
- }
- }
三、删除
-
- public interface UserRepository extends IMongoRepository
{ -
- default void delete(ObjectId id){
-
- this.deleteById(id);
- }
-
- default void delete1(User user){
-
- this.delete(user);
- }
-
- default DeleteResult delete2(ObjectId id){
-
- Query q = new Query(Criteria.where("_id").is(id));
- return this.getMongoOperations().remove(q, getTableName());
- }
- }
四、索引
- public interface UserRepository extends IMongoRepository
{ -
-
- default void createIndex() {
-
- MongoCollection
collection = getMongoOperations().getCollection(getTableName()); - BasicDBObject indexOptions = new BasicDBObject();
- indexOptions.put(User.Fields.name, -1);
- String alarmSummaryId = collection.createIndex(indexOptions);
-
- indexOptions = new BasicDBObject();
- indexOptions.put(User.Fields.age, 1);
- String createAt = collection.createIndex(indexOptions);
-
- }
- }
五、聚合
- public interface UserRepository extends IMongoRepository
{ -
- default Map
listAgeFirstCity(int skip, int limit){ - Criteria criteria = Criteria.where(User.Fields.city).exists(true);
-
- Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
- Aggregation.group(User.Fields.age).first( "$city").as( "city"),
- Aggregation.skip(skip), Aggregation.limit(limit));
-
-
- List
- Map
data = new HashMap<>(); - for(Map
map : list){ - data.put((ObjectId) map.get("_id"), (String)map.get("city"));
- }
- return data;
- }
- }
六、连接
- public interface UserRepository extends IMongoRepository
{ -
-
- default List
list() { -
- LookupOperation lookupOperation= LookupOperation.newLookup().
- from(AdminRepository.instance().getTableName()). //关联从表名
- localField(User.Fields.name). //主表关联字段
- foreignField(Admin.Fields.name).//从表关联的字段
- as("adminInfo"); //查询结果名
-
-
-
- Criteria left = Criteria.where(User.Fields.city).exists(true)
- .andOperator(Criteria.where(User.Fields.age).gte(18),
- Criteria.where(User.Fields.age).lt(65));
- Criteria right = Criteria.where("adminInfo.0").exists(true).and("adminInfo.0.admin").is(1);
- Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(left), lookupOperation,
- Aggregation.match(right), Aggregation.unwind("adminInfo", true));
-
- List
results = this.getMongoOperations().aggregate(aggregation, this.getTableName(), UserVo.class).getMappedResults(); -
- return results;
- }
-
- @Setter
- @Getter
- public static class UserVo extends User{
-
- private Admin adminInfo;
- }
-
- }