我们标准的基础聚合是什么样,来一段,所有都在基础上扩展
db.sysUser.aggregate([
{"$match":{ "_id" : ObjectId("5a389f0510d00e056c73e1c9"),
}},
{"$lookup":{ "from":"sysDefRoleUser", "localField": "_id","foreignField":"roleUser.userId","as":"_joinSysDefRoleUser_"}},
//{"$match":{ "_joinSysDefRoleUser_.srcDef.srcDefId":ObjectId("6324130e62e0ea33fcc8513f"), }},
{"$unwind": {"path":"$_joinSysDefRoleUser_" } },// "$_joinSysDefRoleUser_"
//{"$project":{ "loginName":1 , "examRole":1 , "ifExam":1 , "_joinSysDefRoleUser_": 1, "newSrcDefId": "$_joinSysDefRoleUser_.srcDef.srcDefId",} },
{"$match":{ "_joinSysDefRoleUser_.srcDef.srcDefId":ObjectId("6324130e62e0ea33fcc8513f"), }},
//{"$match":{ "newSrcDefId":ObjectId("6324130e62e0ea33fcc8513f"), }},
//{"$match":{ "newSrcDefId":ObjectId("5dd4f6b0e0ee77720bc9e64b"), }},
])
aggregate.add(new org.bson.Document("$unwind", new org.bson.Document("path", "$" + DFN.joinDocAlias.n())));
org.bson.Document pbUnwind = new org.bson.Document();
org.bson.Document pbPathUnwind = new org.bson.Document()
.append("path", "$" + lookupParentBizAs)
.append("preserveNullAndEmptyArrays", true);
pbUnwind.append("$unwind", pbPathUnwind);
sql.add(pbUnwind);
countSql.add(pbUnwind);
$regex
$lookup
$first
,,
看svn提交记录,总结 这些冷门api使用场景,关键时刻不慌
适用场景:
org.bson.Document bizAddFields = new org.bson.Document("$addFields",
new org.bson.Document("notNullFlowId", new org.bson.Document("$ifNull", Arrays.asList("$flowId", new ObjectId()))));
sql.add(bizAddFields);
countSql.add(bizAddFields);
final String lookupFlowTaskAs = "_join_flowTask";
org.bson.Document lookupCont = new org.bson.Document("from", "flowTask").append("localField", "notNullFlowId").append("foreignField", "flowId").append("as",
lookupFlowTaskAs);
org.bson.Document lookupCond = new org.bson.Document();
lookupCond.append("$lookup", lookupCont);
sql.add(lookupCond);
countSql.add(lookupCond);
org.bson.Document sort = new org.bson.Document("$sort", new org.bson.Document(DFN.common_addTime.n(), -1));
sql.add(sort);
$addFields,
{ $addFields: { <newField>: <expression>, ... } }
$concat
$split
$exists语法: { field: { $exists: <boolean> } }
$$ROOT
@Override
public List<Map> queryBizGroupByOpenSourceProtocol(List<ObjectId> bizIds) {
if (null == bizIds || bizIds.size() == 0) {
return Collections.emptyList();
}
Criteria taskIdsCond = Criteria.where(DFN.biz_id.n())
.in(bizIds).and(DFN.biz_custFieldInfo.dot(SysDefConstants.DEF_ID_FIELD_OPEN_SOURCE_PROTOCOL).n()).ne(null);
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(taskIdsCond),
new AggregationOperation(){
@Override
public Document toDocument(AggregationOperationContext aoc) {
List<String> split = new ArrayList() {{
add(DFN.biz_custFieldInfo.dot(SysDefConstants.DEF_ID_FIELD_OPEN_SOURCE_PROTOCOL).$n());
add(",");
}};
return new Document("$addFields",new Document("coord",new Document("$split",split)));
}
},
Aggregation.unwind("$coord", false),
Aggregation.group("$coord").count().as("count").push("$$ROOT._id").as("rootDataId"),
Aggregation.sort(Sort.by(Sort.Direction.DESC,"count"))
);
AggregationResults<Map> results = this.mongoTemplate.aggregate(agg,
DBT.BIZ.getName(), Map.class);
if (null == results) {
return null;
}
return results.getMappedResults();
}
821

被折叠的 条评论
为什么被折叠?



