MongoDB - Spring Data MongoTemplate使用

Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate类,方便我们通过java代码对MongoDB进行CRUD的操作。

一、增加:save、insert

 T save(T var1);
 T save(T var1, String var2);
1. 传入对象,不传集合名
2. 传入对象和集合名

记录存在则更新,不存在则插入
T insert(T var1);
T insert(T var1, String var2);
Collection insert(Collection var1, Class var2);
Collection insert(Collection var1, String var2);
Collection insertAll(Collection var1);

二、查询:find

Query:查询条件,var2:doc对应的类,var3:doc-name

查一条记录:

@Nullable
 T findOne(Query var1, Class var2);
@Nullable
 T findOne(Query var1, Class var2, String var3);

查多条:

 List find(Query var1, Class var2);
 List find(Query var1, Class var2, String var3);

根据id查:

@Nullable
 T findById(Object var1, Class var2);
@Nullable
 T findById(Object var1, Class var2, String var3);

查出后删除:

@Nullable
 T findAndRemove(Query var1, Class var2);
@Nullable
 T findAndRemove(Query var1, Class var2, String var3);
 List findAllAndRemove(Query var1, String var2);
 List findAllAndRemove(Query var1, Class var2);
 List findAllAndRemove(Query var1, Class var2, String var3);

查出后修改:

@Nullable
 T findAndModify(Query var1, Update var2, Class var3);
@Nullable
 T findAndModify(Query var1, Update var2, Class var3, String var4);
@Nullable
 T findAndModify(Query var1, Update var2, FindAndModifyOptions var3, Class var4);
@Nullable
 T findAndModify(Query var1, Update var2, FindAndModifyOptions var3, Class var4, String var5);

判断是否存在:

boolean exists(Query var1, String var2);
boolean exists(Query var1, Class var2);
boolean exists(Query var1, @Nullable Class var2, String var3);

三、删除:remove

根据doc或query删除

DeleteResult remove(Object var1);
DeleteResult remove(Object var1, String var2);
DeleteResult remove(Query var1, Class var2);
DeleteResult remove(Query var1, Class var2, String var3);
DeleteResult remove(Query var1, String var2);

四、更新

update+insert,和updateFirst一样:

UpdateResult upsert(Query var1, Update var2, Class var3);
UpdateResult upsert(Query var1, Update var2, String var3);
UpdateResult upsert(Query var1, Update var2, Class var3, String var4);

updateFirst 更新符合条件的第一条:

UpdateResult updateFirst(Query var1, Update var2, Class var3);
UpdateResult updateFirst(Query var1, Update var2, String var3);
UpdateResult updateFirst(Query var1, Update var2, Class var3, String var4);

更新符合条件的所有:

UpdateResult updateMulti(Query var1, Update var2, Class var3);
UpdateResult updateMulti(Query var1, Update var2, String var3);
UpdateResult updateMulti(Query var1, Update var2, Class var3, String var4);

五、高级操作

官方文档:https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

5.1 计数Count:

long count(Query var1, Class var2);
long count(Query var1, String var2);
long count(Query var1, @Nullable Class var2, String var3);

5.2 聚集:

Var2:collection name;var3返回的class类型;

1. aggregate:

 AggregationResults aggregate(TypedAggregation var1, String var2, Class var3);
 AggregationResults aggregate(TypedAggregation var1, Class var2);
 AggregationResults aggregate(Aggregation var1, Class var2, Class var3);
 AggregationResults aggregate(Aggregation var1, String var2, Class var3);

2. aggregateStream:

Execute an aggregation operation backed by a Mongo DB Cursor.
返回的是一个被封装过的cursor
 CloseableIterator aggregateStream(TypedAggregation var1, String var2, Class var3);
 CloseableIterator aggregateStream(TypedAggregation var1, Class var2);
 CloseableIterator aggregateStream(Aggregation var1, Class var2, Class var3);
 CloseableIterator aggregateStream(Aggregation var1, String var2, Class var3);

AggregationOperation是一个顶级接口。协助构造Agg

3. Aggregation类:

过滤match
连表lookup:
//from:collection name; localField:当前collection的field;foreignField:from中collection的field;as:连表后的field名称。

  public static LookupOperation lookup(String from, String localField, String foreignField, String as) {
        return lookup(Fields.field(from), Fields.field(localField), Fields.field(foreignField), Fields.field(as));
    }

 public static LookupOperation lookup(Field from, Field localField, Field foreignField, Field as) {
        return new LookupOperation(from, localField, foreignField, as);
    }

展开unwind:(连表后将其作为了一个field的数组,所以需要展开此field。)
UnwindOperation(Field field, boolean preserveNullAndEmptyArrays)

投影project:修改输入文档的结构
多个Aggregation 是按先后顺序执行的,即后面一个在前面一个的基础上作筛选等操作。MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。

5.3 MR:

mapReduce
待补充…

你可能感兴趣的:(数据库)