$addFields $ifNull

我们标准的基础聚合是什么样,来一段,所有都在基础上扩展
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();
	}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值