Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate类,方便我们通过java代码对MongoDB进行CRUD的操作。
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 extends T> var1, Class> var2);
Collection insert(Collection extends T> var1, String var2);
Collection insertAll(Collection extends T> var1);
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);
@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);
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);
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);
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/
long count(Query var1, Class> var2);
long count(Query var1, String var2);
long count(Query var1, @Nullable Class> var2, String var3);
Var2:collection name;var3返回的class类型;
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);
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
过滤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文档在一个管道处理完毕后将结果传递给下一个管道处理。
mapReduce
待补充…